亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用不同的路由但使用相同的組件 VueJS 3 獲取數據

使用不同的路由但使用相同的組件 VueJS 3 獲取數據

慕俠2389804 2023-06-15 15:58:21
請檢查這段代碼:它基本上顯示了兩條路線,使用使用來自 API 的內容的相同組件。主.jsconst router = createRouter({    history: createWebHistory(),    routes: [        {            path: "/route1",            name: "Route1",            component: BaseContent,            meta: {                title: 'Route 1'            }        },        {            path: "/route2",            name: "Route2",            component: BaseContent,            meta: {                title: 'Route2'            }        }    ]});基礎內容.vue  <base-section v-for="entry in this.entries" :key="entry" :title="entry.title">  <template v-slot:content>      <div v-html="entry.content"></div>  </template>    <template v-slot:examples>      <div v-html="entry.examples"></div>    </template>    <template v-slot:code>        {{entry.code}}    </template>  </base-section></template><script>export default {  mounted(){    this.$nextTick(function () {    Prism.highlightAll();    this.getData()  })  },      updated(){    this.$nextTick(function () {    Prism.highlightAll();    this.getData()  })  },  methods:{    getData(){      const url= 'https://example.dev/api/collections/get/'+this.$route.name+'?token=XXXXXXXXX'        fetch(url)    .then(collection => collection.json())    .then(collection => {      const entries = [];            for (const id in collection.entries) {              entries.push({                title: collection.entries[id].Title,                content: collection.entries[id].Content,                examples: collection.entries[id].Examples,                code: collection.entries[id].Code,              });            }            this.entries = entries;    });    }  },  data() {    return {      entries:[]    };  },};</script>問題: 這個解決方案有效。但是有兩件事讓我發瘋。 1st - 內容以一種奇怪的方式表現,當我單擊鏈接以遵循這些路線中的任何一條時,內容在實際呈現正確內容之前在兩條路線內容之間閃爍 2nn - 如果我打開 DEV TOOLS, 我看到content Is CONSTANTLY updating(開發工具代碼選項卡上的部分標簽不斷閃爍紫色,表示更新)。關于我做錯了什么的任何提示?
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

我設法解決了這個問題。經過一番挖掘,我發現我所需要的只是為文件中的組件提供一個唯一:key標識符。<router-view>App.vue

所以我剛剛添加了這個:

<router-view :key="$route.fullPath"></router-view>

它解決了這個問題,因為 Vue 的反應系統知道它需要再次重新加載整個組件,因為鍵是唯一的,而之前它不是。

希望能幫助別人!

問候,T。


查看完整回答
反對 回復 2023-06-15
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號