如何将Vue路由器与Vue集成

seh*_*mel 1 vue.js

我找到了一个如何使用Vue路由器的很好的例子.这是app.js文件:

// Telling Vue to use the router
Vue.use(VueRouter)

// Initializing the router with options
var router = new VueRouter({
    history: false
});

// Redirect certain routes to other routes
router.redirect({
    '/': '/users/list'
})

// Define your routes here.  
// NOTE: You'd normally do something
// like require('./home/index.vue') for the component
router.map({
    // Not found handler
    '*': {
        component: {
            template:
            '<div>' +
            '<h1>Not Found</h1>' +
            '</div>'
        }
    },
    '/users': {
        component: {
            template:
            '<div>' + // Wrap your views in one root html node so that the transitions work
            '<h1>Users</h1>' +
            '<ul class="nav nav-tabs">' +
            '<li><a v-link="/users/list">List</a></li>' +
            '<li><a v-link="/users/create">Create</a></li>' +
            '</ul>' +
            '<br>' +
                // <router-view></router-view> is where the nested sub route views will appear.
                // If you want the transitions to happen here you can copy the attributes on the router-view in codepen's HTML view and paste it here.
            '<router-view></router-view>' +
            '</div>'
        },
        subRoutes: {
            '/list': {
                component: {
                    template:
                    '<div>' +
                    '<ul><li><a v-link="/users/1/profile">Rick James</a></li></ul>' +
                    '</div>'
                }
            },
            '/create': {
                component: {
                    template:
                    '<form>' +
                    '<div class="form-group">' +
                    '<input class="form-control" type="text">' +
                    '</div>' +
                    '<button class="btn btn-primary">Create</button>' +
                    '</form>'
                }
            },
            '/:id': {
                component: {
                    template:
                    '<div>' +
                    '<h1>User Settings</h1>' +
                    '<ul class="nav nav-tabs">' +
                    '<li><a v-link="/users/{{route.params.id}}/profile">Profile</a></li>' +
                    '<li><a v-link="/users/{{route.params.id}}/posts">Posts</a></li>' +
                    '</ul>' +
                    '<br>' +
                    '<router-view></router-view>' +
                    '</div>'
                },
                subRoutes: {
                    '/profile': {
                        component: {
                            template: '<div>Name: Rick James<br>Email: rick@james.com</div>'
                        }
                    },
                    '/posts': {
                        component: {
                            template: '<div><ul><li>Post Name 1</li><li>Post Name 2</li></ul></div>'
                        }
                    }
                }
            }
        }
    },
    '/different': {
        component: {
            template: '<div>' +
            '<h1>Different</h1><p>{{ test }}</p>' +
            '</div>',
            data: function() {
                return {
                    test: 'Hello I am a data variable from Vue.JS'
                }
            }
        }
    },
    '/about': {
        component: {
            template:
            '<div>' +
            '<h1>About</h1>' +
            '<p>' +
            'Well my father was a gambler down in Georgia,<br>' +
            'He wound up on the wrong end of a gun.<br>' +
            'And I was born in the back seat of a Greyhound bus<br>' +
            'Rollin\' down highway 41.<br><br>' +
            'Lord, I was born a ramblin\' man,<br>' +
            'Tryin\' to make a livin\' and doin\' the best I can.<br>' +
            'And when it\'s time for leavin\',<br>' +
            'I hope you\'ll understand,<br>' +
            'That I was born a ramblin\' man.' +
            '</p>' +
            '</div>'
        }
    }
});

// Declaring the app itself

var App = Vue.extend();

// Initializing the whole thing together
router.start(App, '#app')
Run Code Online (Sandbox Code Playgroud)

但我不知道在哪里放置其余的代码.例如,你需要初始化Vue,不是吗?你把你的方法,你对Vue资源的调用等等放在哪里我尝试添加这个:

var app = new Vue({
    el : '#app',
    methods: {
        alertTest : function() {
            alert('hello');
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

但我不知道如何整合.为此alertTest,我v-on在我的一个链接上有一个活动.链接在这里:

 <a class="list-group-item" v-link="/users/list" v-on="click: alertTest">Users</a> 
Run Code Online (Sandbox Code Playgroud)

但事件并没有发生.我觉得我需要将第一个代码块(来自Michael J. Calkins的教程)绑定到第二个代码块中,以便触发事件.我怎么做?我不知道在路由器之外放置应用程序的其余部分.

Pra*_*har 5

这可能为时已晚,无法回答.我最近也接受了Vue.js并有同样的疑问.通过Vue.js文档,Vue实例是使用新的Vue()创建的,当我开始学习vue-router时,这一切都发生了变化.

请注意,当我们将Vue与Vue路由器一起使用时,我们必须考虑一些API更改.

简单的工作流程是:

  • 创建所有组件,如下所示:

    var home=Vue.extend({template:"<div align='center'>Homepage</div>"}); var about=Vue.extend({template:"<div align='center'>About</div>"}); var contact=Vue.extend({template:"<div align='center'>Contact</div>"});

  • 创建你的应用程序:[注意api在这里改变.我们不再做新的Vue()了 var App = Vue.extend({});
  • 创建并注册您的路由器,如: var router = new VueRouter();
  • 注册您的路线: router.map({ "/" : {component : home}, "/home" : {component : home}, "/about" : {component : about}, "/contact" : {component : contact}
    })
  • 初始化您的应用: router.start(App, '#app'); 请注意此步骤.在这里,我们初始化并启动应用程序.而不是常规的Vue api,我们需要注册app中声明的元素,比如新的Vue(el:"#app"),看看api是如何改变的,现在我们用vue注册元素(#app)和上面的语法.

另一件需要注意的事情是.其他Vue选项[如数据,计算,准备等]仍然可以像下面这样提供:

var App = Vue.extend({
data : function(){
    return {
        message : "hello"
    }
}
Run Code Online (Sandbox Code Playgroud)

});

注意 - 1请记住从您的页面调用这些路线<a v-link={path:'/home'}.

注意2请记住将所有v-links放在html范围内的#app,否则你的链接将无法点击.

对于像我这样对javascript世界不熟悉的人,只要坚持工作示例中提供的约定就适合我.