[英]Vue.js - how to pass data to another route?
I am pretty new to Vue.js and have a question.我对 Vue.js 很陌生,有一个问题。
First I have this code to get all the data from my backend application:首先,我有这个代码来从我的后端应用程序中获取所有数据:
var app2 = new Vue({
delimiters: ['%%', '%%'],
el: '#app2',
data: {
articles: []
},
mounted : function()
{
this.loadData();
},
methods: {
loadData: function() {
this.$http.get('/backend/FpArticle/articleApi').then(response => {
// get body data
this.articles = response.body;
}, response => {
// error callback
});
},
},
I guess this is pretty straightforward.我想这很简单。 Now I display the data in articles on the frontend in a table view.现在我在表格视图中显示前端文章中的数据。 So I do something like this:所以我做这样的事情:
<tr v-for="article in articles">
<td>{{ article.name }}</td>
</tr>
This works.这有效。 But now I want to create an edit mask where the user can change some data elements of this article.但现在我想创建一个编辑掩码,用户可以在其中更改本文的某些数据元素。 So I assume to do something like this:所以我假设做这样的事情:
<tr v-for="article in articles">
<td>{{ article.name }}</td>
<td><a href="/article/{{ article.id }}">edit</a></td>
</tr>
So I need another component that takes the id, reads the data of the article, displays it in a form and handles a save event.所以我需要另一个组件来获取 id,读取文章的数据,在表单中显示它并处理保存事件。 I think I know how I resolve the latter part, but how do I do the routing with a new component?我想我知道如何解决后半部分,但是如何使用新组件进行路由? Or is there a better way that is recommended in Vue?或者有没有更好的方法在 Vue 中推荐? Something like this maybe?也许像这样的东西?
<tr v-for="article in articles">
<td>{{ article.name }}</td>
<td><button v-on:click="editArticle(article.id)">edit</button></td>
</tr>
I am grateful for any tips!我很感激任何提示! Thanks!谢谢!
Yes, you're on the right tracks.是的,你在正确的轨道上。 You want to use VueRouter
.你想使用VueRouter
。 You have to configure routes with your components.您必须使用组件配置路由。 For example:例如:
const router = new VueRouter({
routes: [
{ path: '/articles', component: ArticlesList },
{ path: '/articles/:id', component: Article }
]
})
Then, you'll reach your Article
view with <router-link>
(that will be rendered as <a>
tag by default):然后,您将使用<router-link>
到达您的Article
视图(默认情况下将呈现为<a>
标签):
<td><router-link to="'/article/' + article.id">edit</router-link></td>
Also, you need <router-view></router-view>
tag to render your components view.此外,您需要<router-view></router-view>
标签来呈现您的组件视图。 Component matched by the route will be rendered here.路由匹配的组件会在这里渲染。
Everything's on the documentation ;-) https://router.vuejs.org/en/essentials/getting-started.html一切都在文档中;-) https://router.vuejs.org/en/essentials/getting-started.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.