通过管理员模式打开命令行
输入命令 vue init webpack hello-vue
创建一个名字为"hello-vue"的工程
创建时不要安装任何插件,我们自己安装
cd hello-vue
进入我们的工程内部
接下来安装一些依赖
首先安装路由
npm install vue-router --save-dev
安装element-ui -S
npm i element-ui -S
安装依赖
npm install
安装SASS加载器
cnpm install sass-loader node-sass --save-dev
启动测试
npm run -dev
Npm命令解释
npm install moduleName:安装模块到项目目录下
npm install -g moduleName: -g的意思是将模块安装到全局,具体安装到磁盘的哪个位置,要看npm config
prefix的位置
npm install -save moduleName:–save的意思是将模块安装到项目目录下,并在package文件的dependencies节点写入依赖 -S为该命令的缩写
npm install -save-dev moduleName:–save-dev的意思是将模块安装到项目目录下,并在package文件的dependencies节点写入依赖 -D为该命令的缩写
通过idea打开此工程
创建router和views文件夹
在views中放入组件
创建一个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:{
onSubmit(formName) {
//为表单绑定验证功能
this.$refs[formName].validate((valid) =>{
if (valid){
//使用 vue-router路由到指定页面,该方式称之为编程式导航
this.$router.push("/main");
} else {
this.dialogVisible = true;
return false;
}
});
}
}
}
</script>
<style 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 VueRouter from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
//安装路由
Vue.use(VueRouter);
//配置路由导出
export default new VueRouter({
routes:[
{
//路由路径,请求content就会进Content组件
path:'/login',
//组件的跳转
component:Login
},
{
//路由路径,请求content就会进Content组件
path:'/main',
//组件的跳转
component:Main
}
]
});
在main.js中同样导入组件,安装路由,安装elementUI
```java
// 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'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
//安装路由
Vue.use(router);
//安装elementui
Vue.use(ElementUI);
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App)
})
在App.vue中设置路由视图
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
运行
npm run dev
login请求
完成!
参数绑定
以及路由嵌套
<router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link>
{
//路由路径,请求content就会进Content组件
path:'/main',
//组件的跳转
component:Main,
children:[
{path:'/user/profile/:id',
component:UserProfile,
name:'UserProfile',
props:true },
{path:'/user/list',component:UserList}
]
}
数据展示页面,使用props属性绑定数据
<template>
<div>
<h1>用户信息</h1>
<h1>{{id}}</h1>
</div>
</template>
<script>
export default {
name: "UserProfile",
props:['id']
}
</script>
<style scoped>
</style>
重定向:
在index.js中配置一个新的路由路径/goHome,重定向到/main中即可
{
path:'/goHome',
redirect:'/main'
}