|
|
@@ -1,4 +1,4 @@
|
|
|
-<template>
|
|
|
+<!-- <template>
|
|
|
<div>
|
|
|
<h1>Pinia</h1>
|
|
|
<h2>当前值是:{{ num }}--双倍:{{ doubleNum }}</h2>
|
|
|
@@ -79,4 +79,117 @@ onUnmounted(() => {
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
-</style>
|
|
|
+</style> -->
|
|
|
+
|
|
|
+<template>
|
|
|
+ <div class="weather-box">
|
|
|
+ <h1>🌡️ 趣味天气温度模拟器</h1>
|
|
|
+
|
|
|
+ <div class="show-card">
|
|
|
+ <div class="icon">{{ weatherIcon }}</div>
|
|
|
+ <p>
|
|
|
+ 当前实际温度:<span class="temp">{{ num }} ℃</span>
|
|
|
+ </p>
|
|
|
+ <p>
|
|
|
+ 体感温度:<span class="feel">{{ doubleNum }} ℃</span>
|
|
|
+ </p>
|
|
|
+ <p>文字天气:{{ weather }}</p>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div class="btn-group">
|
|
|
+ <button @click="handeleAdd(5)">升温 +5℃</button>
|
|
|
+ <button @click="reduceTemp(5)">降温 -5℃</button>
|
|
|
+ <button
|
|
|
+ @click="
|
|
|
+ num = 38;
|
|
|
+ weather = '极端高温';
|
|
|
+ "
|
|
|
+ >
|
|
|
+ 直接设38℃
|
|
|
+ </button>
|
|
|
+ <button @click="batchUpdate(0, '大雪纷飞')">$patch 一键入冬</button>
|
|
|
+ <button @click="resetAll">重置到初始26℃</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang="ts">
|
|
|
+import { useCountStore } from "./store/count";
|
|
|
+import { storeToRefs } from "pinia";
|
|
|
+import { onUnmounted, onMounted } from "vue";
|
|
|
+
|
|
|
+const countStore = useCountStore();
|
|
|
+
|
|
|
+// 解构响应式数据与方法
|
|
|
+const { num, weather, doubleNum, weatherIcon } = storeToRefs(countStore);
|
|
|
+const { handeleAdd, reduceTemp, batchUpdate, resetAll, autoChangeWeather } =
|
|
|
+ countStore;
|
|
|
+
|
|
|
+// 页面挂载时:读取本地存储恢复温度
|
|
|
+onMounted(() => {
|
|
|
+ const saveTemp = localStorage.getItem("temp");
|
|
|
+ if (saveTemp) {
|
|
|
+ countStore.num = Number(saveTemp);
|
|
|
+ autoChangeWeather();
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 全局监听写在App.vue,App是根组件,页面全程不销毁
|
|
|
+const unWatch = countStore.$subscribe((mutation, state) => {
|
|
|
+ // 持久化保存
|
|
|
+ localStorage.setItem("temp", state.num.toString());
|
|
|
+ console.log("【App根组件监听】变更类型:", mutation.type);
|
|
|
+ console.log("最新温度:", state.num, "天气:", state.weather);
|
|
|
+});
|
|
|
+
|
|
|
+// 根组件理论不会卸载,加上兜底销毁逻辑
|
|
|
+onUnmounted(() => {
|
|
|
+ unWatch();
|
|
|
+});
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped>
|
|
|
+.weather-box {
|
|
|
+ width: 600px;
|
|
|
+ margin: 60px auto;
|
|
|
+ text-align: center;
|
|
|
+ font-family: "Microsoft Yahei";
|
|
|
+}
|
|
|
+.show-card {
|
|
|
+ padding: 30px;
|
|
|
+ border-radius: 16px;
|
|
|
+ background: linear-gradient(135deg, #e0f7ff, #fef9e7);
|
|
|
+ margin-bottom: 30px;
|
|
|
+}
|
|
|
+.icon {
|
|
|
+ font-size: 60px;
|
|
|
+ margin-bottom: 16px;
|
|
|
+}
|
|
|
+.temp {
|
|
|
+ font-size: 28px;
|
|
|
+ color: #e74c3c;
|
|
|
+ font-weight: bold;
|
|
|
+}
|
|
|
+.feel {
|
|
|
+ font-size: 24px;
|
|
|
+ color: #3498db;
|
|
|
+}
|
|
|
+.btn-group {
|
|
|
+ display: flex;
|
|
|
+ gap: 12px;
|
|
|
+ flex-wrap: wrap;
|
|
|
+ justify-content: center;
|
|
|
+}
|
|
|
+button {
|
|
|
+ padding: 10px 16px;
|
|
|
+ border: none;
|
|
|
+ border-radius: 8px;
|
|
|
+ background: #4299e1;
|
|
|
+ color: white;
|
|
|
+ font-size: 15px;
|
|
|
+ cursor: pointer;
|
|
|
+}
|
|
|
+button:hover {
|
|
|
+ background: #3182ce;
|
|
|
+}
|
|
|
+</style>
|