赞
踩
根据之前创建vue-cli项目一样再来一遍 创建项目
1. 创建一个名为 hello-vue 的工程 vue init webpack hello-vue
2. 安装依赖,我们需要安装 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
3. 创建成功后用idea打开,并删除净东西(没用的那堆东西) 创建views和router文件夹用来存放视图和路由
首先创建Main.vue主视图
<template>
<h1>首页</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
之后我们在创建一下Login.vue组件,用于登录。这其中的代码是使用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: { 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>
在router中配置路由!创建index.js。这里都是老套路了,就不做注释了啦!
//导入组件以及路由 import Vue from 'vue' import Router from 'vue-router' import Main from '../components/Main' import Login from '../components/Login' Vue.use(Router); export default new Router({ routes :[ { path : '/main', component: Main }, { path : '/login', component: Login } ] })
在之后我们修改下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 VueRouter from "vue-router"; //扫描路由配置 import router from "./router" //导入elementUI import Vue from 'vue' //导ElementUI以及相关的css import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import App from './App.vue' Vue.config.productionTip = false Vue.use(router); Vue.use(ElementUI); /* eslint-disable no-new */ new Vue({ el: '#app', router, render: h => h(App),//ElementUI规定这样使用 });
最后在APP.vue中添加以下router-view标签,使其可以产生视图。
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
ok,运行跑一下!
npm run dev
真8错。
如果出现错误: 可能是因为sass-loader的版本过高导致的编译错误,当前最高版本是8.0.2,需要退回到7.3.1 ;
去package.json文件里面的 "sass-loader"的版本更换成7.3.1,然后重新cnpm install就可以了;
那有小伙伴该问了,我们想在一个组件中调用其他组件咋整?
比如说我有一个侧边栏,我想点击一下侧边栏的某个链接,然后侧边栏边上的主页面显示我相关的东西?
我们在上面的基础上做一个这个小demo。
首先定义俩组件。
List.vue
<template>
<h1>用户列表</h1>
</template>
<script>
export default {
name: "UserList"
}
</script>
<style scoped>
</style>
Profile.vue
<template>
<h1>用户信息</h1>
</template>
<script>
export default {
name: "UserProfile"
}
</script>
<style scoped>
</style>
然后我们修改一下路由配置中的参数。
在main的路由中增加children,children代表子路由。
import Vue from 'vue' import Router from 'vue-router' import Main from '../components/Main' import Login from '../components/Login' import UserList from '../view/List' import UserProfile from '../view/Profile' Vue.use(Router); export default new Router({ routes :[ { path : '/main', component: Main, children:[ { path: '/usr/profile', component : UserProfile }, { path: '/usr/list', component : UserList } ] }, { path : '/login', component: Login } ] })
我们在修改一下main.vue中的信息。
<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"> <!--插入的地方,这里的to后面的链接改成链接中的--> <router-link to="/usr/profile">个人信息</router-link> </el-menu-item> <el-menu-item index="1-2"> <!--插入的地方,这里的to后面的链接改成路由中的--> <router-link to="/usr/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>
跑一遍看看。
可以的!我们在main.vue中嵌套了其他组件嘿!
正常我们的页面肯定是要传递一些参数的,比如说我点一下个人信息,可以把用户信息传过来吧。
那在vue中如何进行参数的传递了嘞?
这里有两种方式,
方式一:
首先我们在路由设置中添加一个名字属性
children:[
{
path: '/usr/profile/:id',
name: 'UserProfile',
component : UserProfile
},
之后修改一下main.vue中传递的链接。
<!--:to将传递过去的对象与接收的组件绑定,name中存放需要接收的组件,params中存储需要传递的参数值名字以及值-->
<router-link :to="{name:'UserProfile',params:{id:15}}">个人信息</router-link>
最后在组件中使用{{$route.params.id}}接收。
<template>
<div>
<h1>用户信息</h1>
{{$route.params.id}}</div>
</template>
<script>
export default {
name: "UserProfile"
}
</script>
<style scoped>
</style>
方式二:
使用props 减少耦合
首先修改路由配置,添加props=true
{
path: '/usr/profile/:id',
name: 'UserProfile',
component : UserProfile,
props :true
},
在Profile.vue中增加props,使其接收前端跳转传递的参数,在上面直接通过参数名称调
<template> <div> <h1>用户信息</h1> {{id}}</div> </template> <script> export default { props :['id'], name: "UserProfile" } </script> <style scoped> </style>
两种方式输出结果一样的!
OK!那我们更改一下Login页面的跳转逻辑,使main页面可以显示当前登录的用户名
首先修改下main下面跳转页面的push方法,使其在后面拼接我们所要显示的username
this.$router.push("/main/"+this.form.username);
然后修改下路由配置!
添加props=true,并且传递name属性过去。
routes :[
{
path : '/main/:name',
component: Main,
props :true,
在main.vue中接收下,
export default {
props:['name'],
name: "Main"
}
最后找个地儿输出一下!
<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> <span>{{name}}</span> </el-header> <el-main> <!--在这里展示视图--> <router-view /> </el-main> </el-container> </el-container>
可嘿!
404页面的实现非常简单,就是创建一个404组件,然后在路由配置里配置一下*就可以.
{
path: '*',
component : PageNotFound
}
<template>
<h1>Sorry,没有找到该页面哈!</h1>
</template>
<script>
export default {
name: "PageNotFound"
}
</script>
<style scoped>
</style>
这时我们随意输入一个不存在的界面,他就会自动跳到404的组件中。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6iA7L2i9-1603626914071)(E:\学习笔记\typora图床\image-20201025163700750.png)]
beforeRouteEnter:在进入路由前执行
beforeRouteLeave:在离开路由前执行
在Profile.vue中写
export default {
name: "UserProfile",
beforeRouteEnter: (to, from, next) => {
console.log("准备进入个人信息页");
next();
},
beforeRouteLeave: (to, from, next) => {
console.log("准备离开个人信息页");
next();
}
}
参数说明:
to:路由将要跳转的路径信息
from:路径跳转前的路径信息
next:路由的控制参数
next() 跳入下一个页面
next(’/path’) 改变路由的跳转方向,使其跳到另一个路由
next(false) 返回原来的页面
next((vm)=>{}) 仅在 beforeRouteEnter 中可用,vm 是组件实例
1、安装 Axios
cnpm install --save vue-axios
2、main.js引用 Axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
3、准备数据 : 只有我们的 static 目录下的文件是可以被访问到的,所以我们就把静态文件放入该目录下。
数据和之前用的json数据一样 需要的去上述axios例子里
// 静态数据存放的位置
static/mock/data.json
4.在 beforeRouteEnter 中进行异步请求
Profile.vue
export default { //第二种取值方式 // props:['id'], name: "UserProfile", //钩子函数 过滤器 beforeRouteEnter: (to, from, next) => { //加载数据 console.log("进入路由之前") next(vm => { //进入路由之前执行getData方法 vm.getData() }); }, beforeRouteLeave: (to, from, next) => { console.log("离开路由之前") next(); }, //axios methods: { getData: function () { this.axios({ method: 'get', url: 'http://localhost:8080/static/mock/data.json' }).then(function (response) { console.log(response) }) } } }
5、
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。