[英]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.