欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue router路由引用以及Tba切换

程序员文章站 2022-08-15 19:14:18
【1】Vue router1、安装:npm install vue-router或cnpm install vue-router或yarn add vue-router2、在新建router.js中引用如下代码import Vue from ‘vue’import VueRouter from ‘vue-router’Vue.use(VueRouter)3、在src中新建view文件夹,在view新建例如:Home文件夹,在文件夹中新建index.vue输入代码

【1】Vue router
1、安装:npm install vue-router或cnpm install vue-router或yarn add vue-router
2、在新建router.js中引用如下代码
import Vue from ‘vue’
import VueRouter from ‘vue-router’
Vue.use(VueRouter)
3、在src中新建view文件夹,在view新建例如:Home文件夹,在文件夹中新建index.vue
</>

输入代码
<template>
  <div id="home">
       sss
      <Content />

       <router-link to="/home/text1">
        <span>text1</span>
      </router-link> 
      <router-link to="/home/text2">
        <span>text2</span>
      </router-link> 
      <router-link to="/home/text3">
        <span>text3</span>
      </router-link> 
      <router-view></router-view>
     
  </div>

  
</template>
<script>
  import Content from "../../components/Content.vue"
  export default {
    name: "index",
    data(){
        return{
        }
    },
    components:{
        Content,
    },
    methods:{
    }
  }
</script>
<style scoped>
</style>

4、在router.js中设置如下两步
1、import Home from ‘./views/Home/’
2、export default new VueRouter ({
routes: [
{
path: ‘/’,
redirect: ‘/home’ //设置默认指向
},
{
path: ‘/home’,
component: Home
}
]
})

5、在App.vue中的div内引入

<router-view></router-view>

6、在main.js中 import router from ‘./router’

import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

记得在app.vue中使用

 <router-view></router-view>

下面是Tab
1.在Components中新建三个组件text1 2 3
随便写点东西

<template>
  <div>
    text1
  </div>
</template>

<script>

  export default {
    components:{
    },
  }
</script>

<style scoped>

</style>

2.在上面的home中,写有方法home文件夹里面的index中写

<template>
  <div id="home">
       sss
      <Content />

       <router-link to="/home/text1">
        <span>text1</span>
      </router-link> 
      <router-link to="/home/text2">
        <span>text2</span>
      </router-link> 
      <router-link to="/home/text3">
        <span>text3</span>
      </router-link> 
      <router-view></router-view>
     
  </div>

  
</template>
<script>
  import Content from "../../components/Content.vue"
  export default {
    name: "index",
    data(){
        return{
        }
    },
    components:{
        Content,
    },
    methods:{
    }
  }
</script>
<style scoped>
</style>

link链接text1 2 3

3.router.js里面写

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)


import Home from './views/Home/'
import List from './views/List/'

// 组件

import Text1 from './components/Text1'

import Text2 from './components/Text2'

import Text3 from './components/Text3'

export default new VueRouter ({
    // 配置路由信息
    routes: [
      {
        path: '/',
        redirect: '/home'  //设置默认指向
      },
      {
        path: '/home',
        component: Home,
         // Vue中使用children实现路由的嵌套
          // 使用 children 属性,实现子路由,同时,子路由的 path 前面,不要带 / ,
          // 否则永远以根路径开始请求,这样不方便我们用户去理解URL地址
          children:[
            {
                path: '/',
                redirect: 'text1'  //设置默认指向
              },
            {
              path: 'text1',
              component: Text1,
            },
            {
              path: 'text2',
              component: Text2,
            },
            {
              path: 'text3',
              component: Text3,
            }
          ]
      },
      {
        path: '/List',
        component: List
      }
    ]

    
})

本文地址:https://blog.csdn.net/xianyaono1/article/details/107557058