請檢查這段代碼:它基本上顯示了兩條路線,使用使用來自 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(開發工具代碼選項卡上的部分標簽不斷閃爍紫色,表示更新)。關于我做錯了什么的任何提示?
使用不同的路由但使用相同的組件 VueJS 3 獲取數據
慕俠2389804
2023-06-15 15:58:21