Vue-router

Vue-router

Vue Router是Vue.js官方的路由管理器(路径跳转);

包含的功能有:

  • 嵌套的路由/视图表
  • 模块化的,基于组件的路由配置
  • 路由参数、查询、通配符
  • 基于Vue.js过渡系统的视图过渡效果
  • 细粒度的导航控制
  • 带有自动激活的CSS class的链接
  • HTML5历史模式或hash模式,在IE9中自动降级
  • 自定义的滚动条行为
安装
npm install vue-router --save-dev
demo
  1. 将之前的vue-cli项目用idea打开
  2. 清理不用的东西,assert下个的logo,components中的hello.vue
  3. 清理之后只剩下:index.html,App.vue,main.js
  4. 调用关系:index.html调用main.js,main.js调用App.vue

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>myvue</title>
  </head>
  <body>
    <div id="app">
    </div>
    <!-- built files will be auto injected -->
  </body>
</html>

main.js

import Vue from 'vue';
import App from './App';
import router from '../router';//自动扫描里面的路由配置

Vue.config.productionTip = false;

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

App.vue中配置路由连接跳转

<template>
  <div id="app">
    <h1>1213</h1>
    <router-link to="/main">首页</router-link>
    <router-link to="/content">内容页</router-link>
    <router-view/>
  </div>
</template>

<script>


export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

components下自定义自己的组件,首页:Main.vue,内容页:Content.vue

首页和内容页内容基本一样,如下:

<template>
  <h1>内容页</h1>
</template>

<script>
export default {
  name: "Content"
}
</script>

<style scoped>

</style>

src下建路由文件夹router,再键index.js路由配置文件

index.js

import Vue from 'vue';
import VueRouter from "vue-router";

import Content from "../components/Content";
import Main from "../components/Main";

Vue.use(VueRouter);
export default new VueRouter({
  routes: [
    {
      path: '/content',
      component: Content,
      name: 'content'
    },
    {
      path: '/main',
      component: Main,
      name: 'main'
    }
  ]
});

在main.js中使用自己的路由

import Vue from 'vue';
import App from './App';
import router from '../router';

Vue.config.productionTip = false;

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
posted @   jpy  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示