简体   繁体   English

使用不同的路由但使用相同的组件 VueJS 3 获取数据

[英]Fetching data using different routes but using same component VueJS 3

Please check this code: It basically shows two routes, using the same component that consumes content from an API.请检查此代码:它基本上显示了两条路线,使用相同的组件来消费来自 API 的内容。

Main.js主程序

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: "/route1",
            name: "Route1",
            component: BaseContent,
            meta: {
                title: 'Route 1'
            }
        },
        {
            path: "/route2",
            name: "Route2",
            component: BaseContent,
            meta: {
                title: 'Route2'
            }
        }
    ]
});

BaseContent.vue基础内容.vue

  <base-section v-for="entry in this.entries" :key="entry" lang="lang-markup" :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>

THE PROBLEM: This solution works.问题:此解决方案有效。 But there are two things that are making me crazy.但是有两件事让我发疯。 1st - The content is behaving in a strange way, when I click the link to follow any of these routes, the content blinks between both routes content before actually rendering the correct content 2nn - If I open up the DEV TOOLS, I see that the content Is CONSTANTLY updating (the section tag on the code tab of dev tools keeps on flashing purple, meaning updates). 1st - 内容的行为方式很奇怪,当我单击链接以遵循这些路由中的任何一条时,内容在实际呈现正确内容之前在两条路由内容之间闪烁2nn - 如果我打开 DEV TOOLS,我会看到内容不断更新(开发工具代码选项卡上的部分标签不断闪烁紫色,表示更新)。

Any hints about what I am doing wrong?关于我做错了什么的任何提示?

PS: I am new to VUE JS. PS:我是 VUE JS 的新手。

Thank you very much!!!非常感谢!!!

Regards, T.问候,T。

I have managed to solve the problem.我已经设法解决了这个问题。 After some digging, I found out that all I needed was to give a unique :key identifier to the <router-view> component in my App.vue file.经过一番挖掘,我发现我所需要的只是为App.vue文件中的<router-view>组件提供一个唯一的:key标识符。

So i just added this:所以我只是添加了这个:

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

It solves the problem because then Vue's reactivity system knows that it needs to reload the entire component again because the key is unique and before it wasn't.它解决了这个问题,因为 Vue 的反应性系统知道它需要再次重新加载整个组件,因为键是唯一的,并且在它不是之前。

Hope to help others with this!希望能帮助其他人!

Regards, T.问候,T。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM