058——VUE中vue-router之实例操作新闻列表单页面应用与路由别名的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue-router之实例操作新闻列表单页面应用与路由别名的使用</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
<div id="demo">
    <router-view></router-view>
</div>
<script type="text/x-template" id="home">
    <div>
        <li v-for="v in news">
            <router-link :to="{name:'content',params:{id:v.id}}">{{v.title}}</router-link>
        </li>
    </div>
</script>
<script type="text/x-template" id="content">
    <div>
        <h1>{{field.title}}--{{field.id}}</h1>
        <p>
            {{field.content}}
        </p>
        <router-link to="/">返回首页</router-link>
    </div>
</script>
<script>
    var data = [
        {id: 1, title: "php课程", content: "php是个比较牛的技术"},
        {id: 2, title: "java课程", content: "java是个比较牛的技术"}
    ]
    const home = {
        template: "#home",
        data() {
            return {
                news: data
            }
        }
    }
    const content = {
        template: "#content",
        data() {
            return {
                field: {}
            }
        },
        mounted() {
            var id = this.$route.params.id;
            for (let k = 0; k < data.length; k++) {
                if (data[k].id == id) {
                    this.field = data[k];
                }
            }
        }
    }
    let routes = [
        {path: '/', component: home},
        {path: '/content/:id', component: content, name: "content"}
    ];
    //把组件交给路由器:
    let router = new VueRouter({routes});
    new Vue({
        el: "#demo",
        router
    });
</script>
</body>
</html>

  

posted @ 2018-01-17 16:29  生如逆旅,一苇以航  阅读(341)  评论(0编辑  收藏  举报