简体   繁体   English

Vue.js - 如何将数据传递到另一条路线?

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

相关问题 "Vue.js,将数据传递给另一条路线上的组件" - Vue.js, pass data to component on another route 如何将数据从父路由传递到Vue.js嵌套路由中的子路由? - How can you pass data from a parent route to a child route in Vue.js nested routes? Vue.js-如何将数据传递到组件? - Vue.js - How to pass data to component? 如何将 vue.js 值作为参数传递给刀片中的路由 - How to pass a vue.js value as a parameter to a route in blade 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? vue.js 我不知道如何通过道具将数据传递给另一个组件 - vue.js I don't know how to pass data to another component via props 如何将返回数据的值传递给 vue.js 中的另一个脚本 [mounted()] - How to pass value from return data to another script [mounted () ] in vue.js 如何将组件作为道具传递给Vue.js中的另一个组件 - How to pass component as prop to another component in Vue.js 如何将道具传递给 vue.js 中的另一个组件 - How do I pass props to another component in vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM