阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

vue-router实例详细讲解

30次阅读
没有评论

共计 1846 个字符,预计需要花费 5 分钟才能阅读完成。

最近刚刚用 vue 写了个公司项目,使用 vue-cli 构建的,算是中大型项目吧,然后这里想记录并且分享一下其中的知识点,希望对大家有帮助, 后期会逐渐分享;话不多说,直接上代码!!

main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/css/common.css'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '',
  components: {App}
})
router 文件夹里面的 index.js
import Vue from 'vue'
import Router from 'vue-router'
import home from '../components/home'    // 这里可以选择 2 种写法,一种是写在这里,一种是写在 component 里面,看下方代码~

Vue.use(Router)

export default new Router({
      mode:'history',
      routes: [
        {
          path: '/',
          component: home
        },
        {
            path:'/pagevue',
            component:pagevue => require(['../components/childCom/pagevue.vue'], pagevue),
        },
        {
            path:'/nextpagevue',
            component:nextpagevue => require(['../components/childCom/nextpagevue.vue'], nextpagevue),
        }
    ]
})
home.vue
<template>
 <div class="homeMain">
 <div> 我是首页 </div>
 <div class="routerName" @click="clickMe"> 点我进下一个路由 </div>
 </div>
 </template>
 <script>
 export default{data(){return{}
 },
 methods:{clickMe(){this.$router.push({path:'/pagevue'})
 }
 }
 }
 </script>
 <style>
 </style>
pagevue.vue
 <template>
 <div class="homeMain">
 <div> 我是子页面 </div>
 <div class="routerName" @click="returnhome"> 点我继续进下一个路由 </div>
 </div>
 </template>
 <script type="text/javascript">
 export default{
 methods:{returnhome(){this.$router.push({path:'/nextpagevue'})
 }
 }
 }
 </script>
nextpagevue.vue
 <template>
 <div class="homeMain">
 <div> 我是另外一个子页面 </div>
 <div class="routerName" @click="clickGohome"> 点我回到首页 </div>
 </div>
 </template>
 <script type="text/javascript">
 export default{
 methods:{clickGohome(){this.$router.push({path:'/'})
 }
 }
 }
 </script>
common.css
* {
  margin: 0;
  padding: 0; }
.homeMain {
  text-align: center;
  margin-top: 100px; }
  .homeMain .routerName {
    color: #1eb89c;
    border: 1px solid #1eb89c;
    margin-top: 20px; }

/*# sourceMappingURL=common.css.map */

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计1846字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中