1.项目搭建
开始工作参考这个博客
- 创建一一个名为hello-vue 的工程vue init webpack hello-vue
- 安装依赖,我们需要安装vue-router、element-ui、 sass-loader 和node-sass四个插件
进入工程目录:cd hello-vue
安装vue-router:npm install vue-router --save-dev
安装element-ui:npm i element-ui -S
安装依赖:npm install
安装SASS加载器:cnpm install sass-loader node-sass --save-dev
启动测试:npm run dev
2.用idea打开新建的vue项目
删除多余的代码
不废话了直接上代码吧,全部手敲,排错了好久才行。。。
(中途webpack打包的时候可能因为SASS版本过高导致打包失败,就按照上面的指示操作就行,注意:要是使用idea的话要在管理员权限下运行)
项目结构:
index.js
import Vue from 'vue'
import router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
Vue.use(router);
export default new router({
routes: [
{
path: '/main',
component: Main
}, {
path: '/Login',
component: Login
}
]
});
Login.vue
<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: {
handleClose: function () {
console.log("Handle Close,空函数");
},
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>
Main.vue
<template>
<h1>首页</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
App.vue
<template>
<div id="app">
<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);
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
render: h => h(App)
});
效果:
3.路由嵌套
就是界面中嵌套了其他界面
先看效果:
项目结构:
更改了的代码:
Main.vue
<template>
<div>
<el-container>
<!--aside 侧边栏-->
<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>
<!--界面内容在 router-view 中显示-->
<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: #69a4f3;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
index.js
import Vue from 'vue'
import router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
import UserList from '../views/user/List'
import UserProfile from '../views/user/Profile'
Vue.use(router);
export default new router({
routes: [
{
path: '/main',
component: Main,
//嵌套路由
children: [
{path: '/user/list',component: UserList},
{path: '/user/profile',component: UserProfile},
]
}, {
path: '/Login',
component: Login
}
]
});
Profile.vue
<template>
<h1>个人信息</h1>
</template>
<script>
export default {
name: "Profile"
}
</script>
<style scoped>
</style>
List.vue
<template>
<h1>用户列表</h1>
</template>
<script>
export default {
name: "List"
}
</script>
<style scoped>
</style>
4.参数传递
实现参数传递,主要更改三处,
- Main.vue 在router-link标签中,绑定路由的同时传递参数,绑定的路由要以路由的name进行绑定:
<!--v-bind绑定,name 后传递路由的name值,params后是传递的参数-->
<router-link v-bind:to="{name:'profile',params:{id:1}}">个人信息</router-link>
- 路由配置,要配置路径中接收参数的地方,和路由的name
- 界面显示的地方(方式一)
<div>
{{ $route.params.id }}
</div>
一定要将组件包裹在一个div或者其他组件中,否则会报错
- 界面显示的地方(方式二)通过 props 解耦
首先,路由要配置: props: true
Main.vue 界面不变
在显示的界面进行绑定:
5.重定向
redirect 后是重定路径,’ / goHome ’ 是重定向的路由
6.去除路径中的 ’ # ’
路由配置默认为 hash,需要手动配置为 history 还不会显示 #
7.配置404界面
在没有配置404接麦你的情况下,访问不存在的界面没有任何显示,就像这样:
新建一个 NotFound.vue 界面,并配置到路由中:
<template>
<div>
<h1>404,你要访问的界面不存在</h1>
</div>
</template>
<script>
export default {
name: "NotFound"
}
</script>
<style scoped>
</style>
路由配置:
效果:
8.钩子函数
beforeRouteEnter: (to,from,next) => {
console.log('进入路由')
next();
},
beforeRouteLeave: (to,from,next) => {
console.log('离开路由')
next();
}
在钩子函数中使用异步请求:
安装 axios
cnpm install axios -s
在main.js中引入
Axios 官方文档
npm install --save axios vue-axios(建议使用cnpm)
将下面代码加入入口文件:
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
按照这个顺序分别引入这三个文件: vue, axios and vue-axios
准备json文件:
{
"name":"狂神说java",
"url": "http://baidu.com",
"page": 1,
"isNonProfit":true,
"address": {
"street": "含光门",
"city":"陕西西安",
"country": "中国"
},
"links": [
{
"name": "B站",
"url": "https://www.bilibili.com/"
},
{
"name": 4399,
"url": "https://www.4399.com/"
},
{
"name": "百度",
"url": "https://www.baidu.com/"
}
]
}
运行查看是否可以访问到json文件:
代码:
Profile.vue
<template>
<div>
<h1>个人信息</h1>
{{ id }}
</div>
</template>
<script>
export default {
props: ['id'],
name: "Profile",
beforeRouteEnter: (to, from, next) => {
console.log('进入路由')
next(vm => {
vm.getData();
});
},
beforeRouteLeave: (to, from, next) => {
console.log('离开路由')
next();
},
methods: {
getData: function () {
this.axios({
method: 'get',
url: 'http://localhost:8080/static/mock/data.json'
}).then(function (response) {
console.log(response)
})
}
}
}
</script>
<style scoped>
</style>
获取json数据并渲染到界面:
<template>
<div>
<h1>个人信息</h1>
{{ id }}
<div>{{ info.name }}</div>
<div>{{ info.address.country }}</div>
<a v-bind:href="info.url">点击</a>
</div>
</template>
<script>
export default {
props: ['id'],
name: "Profile",
data() {
return {
//请求的返回参数格式,必须和json格式一样(可以不都写,但是写了的格式必须相同)
info: {
name: null,
address: {
street: null,
city: null,
country: null
},
url: null
}
}
},
beforeRouteEnter: (to, from, next) => {
console.log('进入路由')
next(vm => {
vm.getData();
});
},
beforeRouteLeave: (to, from, next) => {
console.log('离开路由')
next();
},
methods: {
getData: function () {
this.axios({
method: 'get',
url: 'http://localhost:8080/static/mock/data.json'
}).then((response)=>{
this.info = response.data
console.log(response.data)
})
}
}
}
</script>
<style scoped>
</style>
完