如何将Vue路由器与Vue集成

时间:2015-09-24 23:34:10

标签: 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')

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

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

但我不知道如何整合。对于alertTest,我在其中一个链接上发生了v-on事件。这是链接:

 <a class="list-group-item" v-link="/users/list" v-on="click: alertTest">Users</a> 

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

3 个答案:

答案 0 :(得分:5)

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

请注意,当我们将Vue与Vue-router一起使用时,我们必须考虑一些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:&#34; #app),看看api是如何改变的,现在我们用vue注册元素(#app),语法如上。

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

var App = Vue.extend({
data : function(){
    return {
        message : "hello"
    }
}

});

注意事项1 请务必从您的网页调用这些路线,例如<a v-link={path:'/home'}

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

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

答案 1 :(得分:4)

我不确定,但这只是正常工作(使用Vue.extend({});代替new Vue({});):

var App = Vue.extend({
    methods: {
        alertTest : function() {
            alert('hello');
        }
    }
});
router.start(App, '#app')

答案 2 :(得分:1)

这是一个简单的例子http://vue.tigerb.cn/

使用

"animate.css": "^3.5.2",
"fastclick": "^1.0.6",
"fetch": "^1.0.1",
"font-awesome": "^4.7.0",
"ratchet-npm": "^2.0.4",
"vue": "^2.0.0",
"vue-infinite-scroll": "^2.0.0",
"vue-progressbar": "^0.7.0",
"vue-router": "^2.0.0",
"vuex": "^2.0.0",
"whatwg-fetch": "^2.0.1"
相关问题