在自己保存目录E:/course/Vue下输入命令创建项目:
第一个第二个使用默认,第三个输入kuangshen,第四个选择第一项:
下一步全部选择:n
初始化项目就完成了:
在安装各种插件和组件:按照上面的命令一次输入
安装vue-router:改为cnpm install vue-router@3.1.3 --save-dev,其他正常输入:
- <template>
- <h1>首页</h1>
- </template>
-
- <script>
- export default {
- name: "Main"
- }
- </script>
-
- <style scoped>
-
- </style>
- <template>
- <div>
- <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
- <h3 class="login-title">欢迎 登录</h3>
- <el-form-item label=" 账号" prop="username">
- <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
- </el-form-item>
- <el-form-item label=" 密码" prop="password">
- <el-input type="password" placeholder=" 请输入密码" v-model="form.password"/>
- </el-form-item>
- <el-form-item>
- <el-button type="primary" v-on:click="onSubmit( 'loginForm' )">登录</el-button>
- </el-form-item>
- </el-form>
- <el-dialog
- title="温馨提示"
- :visible.sync="dialogVisible"
- width="30%"
- :before-close="handLeClose">
- <span>请输入账号和密码</span>
- <span slot="footer" class="dialog- footer">
- <el-button type="primary" @click="dialogVisible = false">确定</el-button>
- </span>
- </el-dialog>
- </div>
- </template>
-
- <script>
- export default {
- name: "Login",
- data() {
- return {
- form: {
- username: '',
- password: ''
- },
- //表单验证,需要在el-form-item 元素中增加prop 属性
- rules: {
- username: [
- {required: true, message: " 账号不可为空", trigger: 'blur'}
- ],
- password: [
- {required: true, message: " 密码不可为空 ", trigger: 'blur'}
- ]
- },
- //对话框显示和隐藏
- dialogVisible: false
- }
- },
- methods: {
- onSubmit(formName) {
- //为表单绑定验证功能
- this.$refs[formName].validate((valid) => {
- if (valid) {//只要不为空就通过
- //使用vue-router路由到指定页面,该方式称之为编程式导航
- this.$router.push("/main");
- } else {
- this.dialogVisible = true;
- return false;
- }
- });
- }
- }
- }
- </script>
-
-
- <style lang="scss" scoped>
- .login-box {
- border: 1px solid #DCDFE6;
- width: 350px;
- margin: 180px auto;
- padding: 35px 35px 15px 35px;
- border-radius: 5px;
- -webkit-border-radius: 5px;
- -moz-border-radius: 5px;
- box-shadow: 0 0 25px #909399;
- }
-
- .login-title {
- text-align: center;
- margin: 0 auto 40px auto;
- color: #303133;
- }
- </style>
index.js
的vue-router路由配置文件- import Vue from 'vue'
- import Router from 'vue-router'
-
- //导入组件
- import Main from '../view/Main'
- import Login from '../view/Login'
-
- Vue.use(Router)
-
- export default new Router({
- routes:[
- {
- path:'/main',
- component:Main
- },
- {
- path:'/login',
- component:Login
- }
- ]
- });
- <template>
- <div id="app">
- <router-link to="/login">login</router-link>
- <router-view></router-view>
- </div>
- </template>
-
- <script>
-
-
- export default {
- name: 'App',
-
- }
- </script>
-
-
main.js
中配置路由-
- import Vue from 'vue'
- import App from './App'
- import router from './router'
-
- import ElementUI from 'element-ui'
- import 'element-ui/lib/theme-chalk/index.css'
-
- //安转路由
- Vue.use(router)
- Vue.use(ElementUI)
-
- new Vue({
- el: '#app',
- router,
- render: h => h(App) //ElementUI
- })
App.vue:
- <template>
- <div id="app">
- <router-link to="/login">login</router-link>
- <router-view></router-view>
- </div>
- </template>
-
- <script>
-
-
- export default {
- name: 'App',
-
- }
- </script>
-
-
index.html;:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>hello-vue</title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
说明: 如果出现错误: 可能是因为sass-loader的版本过高导致的编译错误 ,可以去package.json文件中把sass-loder的版本降低,也有可能是node-sass版本过高,看报错内容修改相应的版本
修改版本后需要重新在项目中下载:
重新启动项目:
点击login或者在控制台输入Login:
只要内容不为空就能进行跳转到Main:
不输入密码:
路由嵌套:
嵌套路由又称子路由,在实际应用中,通常由多层嵌套的组件组合而成
- <template>
- <h1>个人信息</h1>
- </template>
-
- <script>
- export default {
- name: "Profile"
- }
- </script>
-
- <style scoped>
-
- </style>
- <template>
- <h1>用户列表</h1>
- </template>
-
- <script>
- export default {
- name: "List"
- }
- </script>
-
- <style scoped>
-
- </style>
- <template>
- <div>
- <el-container>
- <el-aside width="200px">
- <el-menu :default-openeds="['1']">
-
- <el-submenu index="1">
- <template slot="title"><i class="el-icon-caret-right"></i>用户管理</template>
- <el-menu-item-group>
- <el-menu-item index="1-1">
- <!--插入的地方-->
- <router-link to="/user/profile">个人信息</router-link>
- </el-menu-item>
- <el-menu-item index="1-2">
- <!--插入的地方-->
- <router-link to="/user/list">用户列表</router-link>
- </el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- <el-submenu index="2">
- <template slot="title"><i class="el-icon-caret-right"></i>内容管理</template>
- <el-menu-item-group>
- <el-menu-item index="2-1">分类管理</el-menu-item>
- <el-menu-item index="2-2">内容列表</el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- </el-menu>
- </el-aside>
-
- <el-container>
- <el-header style="text-align: right; font-size: 12px">
- <el-dropdown>
- <i class="el-icon-setting" style="margin-right: 15px"></i>
- <el-dropdown-menu slot="dropdown">
- <el-dropdown-item>个人信息</el-dropdown-item>
- <el-dropdown-item>退出登录</el-dropdown-item>
- </el-dropdown-menu>
- </el-dropdown>
- </el-header>
-
- <el-main>
- <!--在这里展示视图-->
- <router-view />
- </el-main>
-
- </el-container>
- </el-container>
- </div>
- </template>
- <script>
- export default {
- name: "Main"
- }
- </script>
- <style scoped lang="scss">
- .el-header {
- background-color: #B3C0D1;
- color: #333;
- line-height: 60px;
- }
- .el-aside {
- color: #333;
- }
- </style>
添加了组件,修改index.js:
//嵌套路由 children:[ //:id {path:'/user/profile/:id',name:'UserProfile',component:UserProfile,props:true}, {path:'/user/list',component:UserList} ]
- import Vue from 'vue'
- import Router from 'vue-router'
-
- //导入组件
- import Main from '../view/Main'
- import Login from '../view/Login'
-
- import UserList from '../view/user/List'
- import UserProfile from '../view/user/Profile'
-
-
- Vue.use(Router)
-
- export default new Router({
- routes:[
- {
- path:'/main',
- component:Main,//嵌套路由
- children:[
- {path:'/user/profile',component:UserProfile},
- {path:'/user/list',component:UserList}
- ]
- },
- {
- path:'/login',
- component:Login
- }
- ]
- });
点击登录后:
点击个人信息:
参数传递和重定向
参数传递
方法一
{path:'/user/profile/:id',name:'UserProfile',component:UserProfile,props:true},
- import Vue from 'vue'
- import Router from 'vue-router'
-
- //导入组件
- import Main from '../view/Main'
- import Login from '../view/Login'
-
- import UserList from '../view/user/List'
- import UserProfile from '../view/user/Profile'
-
-
- Vue.use(Router);
-
- export default new Router({
- routes:[
- {
- path:'/main',
- component:Main,//嵌套路由
- children:[
- //:id
- {path:'/user/profile/:id',name:'UserProfile',component:UserProfile},
- {path:'/user/list',component:UserList}
- ]
- },
- {
- path:'/login',
- component:Login
- }
- ]
- });
Main.vue添加:
<router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
- <template>
- <div>
- <el-container>
- <el-aside width="200px">
- <el-menu :default-openeds="['1']">
-
- <el-submenu index="1">
- <template slot="title"><i class="el-icon-caret-right"></i>用户管理</template>
- <el-menu-item-group>
- <el-menu-item index="1-1">
- <!--插入的地方-->
- <!--name:传组件名 params:传递参数-->
- <router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
- </el-menu-item>
- <el-menu-item index="1-2">
- <!--插入的地方-->
- <router-link to="/user/list">用户列表</router-link>
- </el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- <el-submenu index="2">
- <template slot="title"><i class="el-icon-caret-right"></i>内容管理</template>
- <el-menu-item-group>
- <el-menu-item index="2-1">分类管理</el-menu-item>
- <el-menu-item index="2-2">内容列表</el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- </el-menu>
- </el-aside>
-
- <el-container>
- <el-header style="text-align: right; font-size: 12px">
- <el-dropdown>
- <i class="el-icon-setting" style="margin-right: 15px"></i>
- <el-dropdown-menu slot="dropdown">
- <el-dropdown-item>个人信息</el-dropdown-item>
- <el-dropdown-item>退出登录</el-dropdown-item>
- </el-dropdown-menu>
- </el-dropdown>
- </el-header>
-
- <el-main>
- <!--在这里展示视图-->
- <router-view />
- </el-main>
-
- </el-container>
- </el-container>
- </div>
- </template>
- <script>
- export default {
- name: "Main"
- }
- </script>
- <style scoped lang="scss">
- .el-header {
- background-color: #B3C0D1;
- color: #333;
- line-height: 60px;
- }
- .el-aside {
- color: #333;
- }
- </style>
<!--name是组件的名字 params是传的参数 如果要传参数的话就需要用v:bind:来绑定--> <router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
说明: 此时我们在Main.vue中的route-link位置处 to 改为了 :to,是为了将这一属性当成对象使用,注意 router-link 中的 name 属性名称 一定要和 路由中的 name 属性名称 匹配,因为这样 Vue 才能找到对应的路由路径
接收参数:
Profile.vue
- <template>
- <div>
- <h1>个人信息</h1>
- {{$route.params.id}}
- </div>
-
- </template>
-
- <script>
- export default {
- name: "Profile"
- }
- </script>
-
- <style scoped>
-
- </style>
更改id为2:
方法二 使用props 减少耦合
{path:'/user/profile/:id',name:'UserProfile',component:UserProfile,props:true},
- import Vue from 'vue'
- import Router from 'vue-router'
-
- //导入组件
- import Main from '../view/Main'
- import Login from '../view/Login'
-
- import UserList from '../view/user/List'
- import UserProfile from '../view/user/Profile'
-
-
- Vue.use(Router);
-
- export default new Router({
- routes:[
- {
- path:'/main',
- component:Main,//嵌套路由
- children:[
- //:id
- {path:'/user/profile/:id',name:'UserProfile',component:UserProfile,props:true},
- {path:'/user/list',component:UserList}
- ]
- },
- {
- path:'/login',
- component:Login
- },
- {// 重定向
- path:'/goHome',
- redirect:'/main'
- }
- ]
- });
- <template>
- <div>
- <h1>个人信息</h1>
- <!--{{$route.params.id}}-->
- {{id}}
- </div>
-
- </template>
-
- <script>
- export default {
- props:['id'],
- name: "Profile"
- }
- </script>
-
- <style scoped>
-
- </style>
重定向
Vue 中的重定向是作用在路径不同但组件相同的情况,用于跳转路径
{// 重定向 path:'/goHome', redirect:'/main' }
- import Vue from 'vue'
- import Router from 'vue-router'
-
- //导入组件
- import Main from '../view/Main'
- import Login from '../view/Login'
-
- import UserList from '../view/user/List'
- import UserProfile from '../view/user/Profile'
-
-
- Vue.use(Router);
-
- export default new Router({
- routes:[
- {
- path:'/main',
- component:Main,//嵌套路由
- children:[
- //:id
- {path:'/user/profile/:id',name:'UserProfile',component:UserProfile,props:true},
- {path:'/user/list',component:UserList}
- ]
- },
- {
- path:'/login',
- component:Login
- },
- {// 重定向
- path:'/goHome',
- redirect:'/main'
- }
- ]
- });
Main.vue,添加:
<el-menu-item index="1-3">
<!--插入的地方-->
<router-link to="/goHome">返回首页</router-link>
</el-menu-item>
- <template>
- <div>
- <el-container>
- <el-aside width="200px">
- <el-menu :default-openeds="['1']">
-
- <el-submenu index="1">
- <template slot="title"><i class="el-icon-caret-right"></i>用户管理</template>
- <el-menu-item-group>
- <el-menu-item index="1-1">
- <!--插入的地方-->
- <!--name:传组件名 params:传递参数-->
- <router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
- </el-menu-item>
- <el-menu-item index="1-2">
- <!--插入的地方-->
- <router-link to="/user/list">用户列表</router-link>
- </el-menu-item>
-
- <el-menu-item index="1-3">
- <!--插入的地方-->
- <router-link to="/goHome">回到首页</router-link>
- </el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- <el-submenu index="2">
- <template slot="title"><i class="el-icon-caret-right"></i>内容管理</template>
- <el-menu-item-group>
- <el-menu-item index="2-1">分类管理</el-menu-item>
- <el-menu-item index="2-2">内容列表</el-menu-item>
- </el-menu-item-group>
- </el-submenu>
-
- </el-menu>
- </el-aside>
-
- <el-container>
- <el-header style="text-align: right; font-size: 12px">
- <el-dropdown>
- <i class="el-icon-setting" style="margin-right: 15px"></i>
- <el-dropdown-menu slot="dropdown">
- <el-dropdown-item>个人信息</el-dropdown-item>
- <el-dropdown-item>退出登录</el-dropdown-item>
- </el-dropdown-menu>
- </el-dropdown>
- </el-header>
-
- <el-main>
- <!--在这里展示视图-->
- <router-view />
- </el-main>
-
- </el-container>
- </el-container>
- </div>
- </template>
- <script>
- export default {
- name: "Main"
- }
- </script>
- <style scoped lang="scss">
- .el-header {
- background-color: #B3C0D1;
- color: #333;
- line-height: 60px;
- }
- .el-aside {
- color: #333;
- }
- </style>