vue组件路由实例(二)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>aaa</title>
    <style>
    .active{
        color:red;
    }
    </style>
</head>
<body>
<div id="app">
    <router-link to="/"  >首页</router-link>
    <router-link to="/news" >新闻</router-link>
    <hr>
    <!--作用类似于占位符-->
    <router-view></router-view>
</div>

<template id="news">
    <div>
        <h1>新闻</h1>
        <router-link to="news/hotNews">热点新闻</router-link>
        <router-link to="/nowNews">实时新闻</router-link>
        <hr>
        <router-view></router-view>
    </div>
</template>


<template id="home">
    <div>
        <h1>首页</h1>
    </div>
</template>


<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/vue-router.js"></script>
<script type="text/javascript">
  /*  Vue.component("news",{
        template:"#news"

    });
    Vue.component("home",{
        template:"#home"
    });*/
    //用定义变量的方式定义组件的配置项
    var Home = {
        name:"home",
        template:"#home"
    };
    var News = {
        name:"news",
        template:"#news"
    };
    var HotNews ={
        name:"hotnews",
        template:"<div><h1>热点新闻</h1></div>"
    }
  var NowNews ={
      name:"hotnews",
      template:"<div><h1>今日新闻</h1></div>"
  }

    //定义路由关系对象(将配置项与路径进行关联)
    const routes =[
        {path:"/",component:Home},
        {path:"/news",component:News,
        children:[
            {path:"hotNews",component:HotNews},
            {path:"/nowNews",component:NowNews}
        ]},

    ];
    //传入配置项,实例路由和组件(将关联后的参数导入到路由实例中)
    const router = new VueRouter({
        //routes:routes
        routes,
       /* linkActiveClass:"active",*/
    });
    //在容器中注入路由 注册路由 (由路由实例注册到vue实例中)
    new Vue({
        el:'#app',
        //router:router
        router
    });
</script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值