首页 > 其他分享 >vue之vue-router路由

vue之vue-router路由

时间:2022-12-09 12:36:06浏览次数:32  
标签:vue js Vue import router 路由


vue-router简介:

Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:

  • 嵌套的路由/视图表
  • 模块化的、基于组件的路由配置
  • 路由参数、查询、通配符
  • 基于 Vue.js 过度系统的视图过度效果
  • 细粒度的导航控制
  • 带有自动激活的 CSS class 的链接
  • HTML5 历史模式活 hash 模式,在 IE9 中自动降级
  • 自定义的滚动条行为

vue-router的安装:

vue-router 是安装在项目当中 node_modules 文件下面,先查看下是否存在。

vue-router 是一个插件包,所有我们还是需要用 npm/cnpm 进行安装,打开命令行工具,在你项目目录下,进行执行命令。

npm install vue-router --save-dev

如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:

import Vue from ‘vue’
import VueRouter from ‘vue-router’
//使用路由
Vue.use(VueRouter );

示例:

创建一个vue项目之后,目录结构!

Main.vue,Content.vue等

<template>
<h1>Say Hi , Main !</h1>
</template>

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

<style scoped>

</style>

index.js文件(src下router中的)

import Vue from 'vue'
import Router from 'vue-router'

import Main from '../components/Main'
import Content from '../components/Content'
import BigJi from '../components/BigJi'

Vue.use(Router)

export default new Router({
routes: [
{
path: '/main',
name: 'main',
component: Main
},
{
path: '/content',
name: 'content',
component: Content
}
]
})

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'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
el: '#app',
//配置路由
router,
components: { App },
template: '<App/>'
})

App.vue

<template>
<div id="app">
<img src="./assets/logo.png">
<router-link to="/main">首页</router-link>
<router-link to="/content">内容</router-link>
</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>


标签:vue,js,Vue,import,router,路由
From: https://blog.51cto.com/u_15907536/5924859

相关文章

  • vue之element-ui
    注意:命令行都要使用管理员模式运行第一步:创建vue工程下面处理项目,创建安装依赖等,都是在命令行进行处理。创建一个工程(vue)vueinitwebpackvue安装依赖,我们需要安装vue-ro......
  • vue3 Vuex使用
    首先src下要有src\store\index.jsindex.js先引入creatStoreimport{createStore}from'vuex'exportdefaultcreateStore({state(){return{......
  • 基于Python+Django+Vue+MYSQL的社团管理系统
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • Avue 在table表中悬浮显示数据
      <templateslot-scope="scope"slot="details"><el-popovertrigger="hover"placement="top"><!--客户名称、原数据、更新后数据-......
  • 原生开发小程序 和 wepy 、 mpvue 对比
    1.三者的开发文档以及介绍:原生开发小程序文档:​​点此进入​​​ wepy开发文档:​​​点此进入​​​ mpvue开发文档:​​​点此进入​​2.三者的简单对比:以下用一张图来......
  • vue 搜索框高亮
    核心代码keySign(title){lets=this.text;//搜索框的值(您要标红的关键字)varstr=title;//列表标题(原文本)//去除中间空格且字符之......
  • 第一个vue-cli程序
    什么是vue-clivue-cli官方提供的一个脚手架。用于快速生成一个vue的项目模板预先定义好的目录结构及基础代码,就好比在创建Maven项目时可以选择创建一个骨架项目......
  • vue3.x keep-alive 写法
     <template><divid="app"class="container"><router-viewv-slot="{Component}"><keep-alive:include="['peopleList','addressList'......
  • Element UI vue 上传文件、下载模板
    样式<el-uploadclass="upload-demo"action="/api/file/upload":on-remove="handleRemove"multipleref="uplpadFile":limit="1":on-ex......
  • vue的安装
    Vue-cli是vue官方出品的快速构建单页应用的脚手架,这里牵扯的东西很多,有webpack,npm,nodejs,babel等等。官网:https://cli.vuejs.org/guide/GitHub:https://github.com/vuejs/v......