简体   繁体   English

Vue/Vuecli3 - 如何使用参数从一个组件路由到另一个组件

[英]Vue/Vuecli3 - How to route from one component to another with parameters

I'm running into an issue when trying to redirect from one component to another.尝试从一个组件重定向到另一个组件时遇到问题。 It appears that it's not routing to the URL thats specified in my router to the desired component and is staying on my home page instead.它似乎没有路由到我的路由器中指定的 URL 到所需的组件,而是停留在我的主页上。 I can't figure out where the error is occuring.我不知道错误发生在哪里。

I'm using the Vue CLI version 3.我正在使用 Vue CLI 版本 3。
Below is my index.js, Home.vue and Model.vue下面是我的 index.js、Home.vue 和 Model.vue

Then under that is images of the Home.vue then it shows what happens when I try to redirect to the link in my href tag.然后是 Home.vue 的图像,然后它显示了当我尝试重定向到我的 href 标签中的链接时发生的情况。
You can see that it's not going to the other component and it's staying on my home page.您可以看到它不会转到其他组件,而是留在我的主页上。

Any ideas on whats causing the issue here?关于什么导致这里问题的任何想法? Thanks!谢谢!

/router/index.js /路由器/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Homefrom '@/components/Home'
import Model from '@/components/Model'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/model/:model_tag_name',
      name: 'Model',
      component: Model
      // props: true 
    }
  ]
})  

/components/Home.vue /组件/Home.vue

<template>
  <div class="hello container-fluid">
    <h1>{{msg}}</h1>
    <div class="row">
      <div class="col-4 text-left">
        <ol>
          <li v-for="tag in tags" v-bind:key="tag.model_tag_name">
            <a :href="'/model/'+ tag.model_tag_name"> {{tag.model_tag_name}}</a>
          </li>
        </ol>
      </div>
      <div class="col-8">
        <p>Data</p>
      </div>
    </div>
  </div>

</template>

<script>
var axios = require('axios');

export default {
  name: 'Home',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      tags: []
    }
  },
  mounted: function() {
    var url = 'http://10.0.0.5:5000';
    console.log(url)
    axios.get(url + '/')
      .then((response) => {
        console.log(response.data);
        this.tags = [{"model_tag_name": response.data}];
      })
      .catch(function(error) {
        console.log(error);
      });
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

a {
  color: #42b983;
}
</style>

/components/Model.vue /组件/Model.vue

<template>
  <div class="container-fluid">
    <h1> Model </h1>
  </div>

</template>

<script>
var axios = require('axios');

export default {
  name: 'Model',
  data () {
    return {
      model_tag_name: this.$route.params.model_tag_name
    }
  }

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

a {
  color: #42b983;
}
</style>

http://localhost:8080/ http://localhost:8080/ 主页

Then this is what happenes when I click the href link on the home page.这就是当我单击主页上的 href 链接时发生的情况。 It's redirecting back to the home page even though the URL matches the routerview for Model.vue即使 URL 与 Model.vue 的路由器视图匹配,它也会重定向回主页下一页

Pleas update this code in /components/Home.vue请在 /components/Home.vue 中更新此代码

   <li v-for="tag in tags" v-bind:key="tag.model_tag_name">
<router-link :to="{ name: 'Model', params: { model_tag_name: tag.model_tag_name}}"> 
 {{tag.model_tag_name}}</router-link>
   </li>

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

相关问题 如何在 router.push() (Vue.js) 中将参数从一个组件传递到另一个组件? - How to pass parameters from one component to another in router.push() (Vue.js)? 如何强制将一个Vue组件从另一个组件重新渲染 - How to force rerender of one Vue component from another Vue JS 3:如何将数据从一个组件传递到另一个组件? - Vue JS 3: How to pass data from one component to another other? 如何在Vue JavaScript中将数据从一个组件恢复到另一个组件 - How to recover data from one component to another in vue JavaScript 如何将计算参数从一个 Vue 组件传递到另一个? - How to pass a computed parameter from one Vue component to another? 如果 Vue 3 中的条件为真,如何将样式从一个组件传递到另一个组件 - How to pass a style from one component to another if condition is true in Vue 3 如何在vue中将数组从一个组件发送到另一个组件 - How to send array from one component to another in vue 来自一个Vue组件的数据会影响另一个组件 - Data from one Vue component affecting another 如何将参数从一个 React 组件导入到另一个组件 - How to import parameters from one React component to another 我如何路由来自同一个组件的多个实例并在 vue 中保持 state - how can i route multi instance that are from the same one component and keep themself state in vue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM