一、创建hello-vue工程
vue init webpack hello-vue
二、安装依赖
2.1、以管理员模式进入工厂目录
2.2、安装vue-router
npm install vue-router --save-dev
2.3、安装element-ui
npm i element-ui -S
2.4、安装依赖
npm install
2.5、安装SASS加载器
cnpm install sass-loader node-sass --save-dev
2.6、启动测试
2.7、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文件的devDependencies节点写入依赖,-D 为该命令的缩写
三、开发
3.1、用idea打开工程
3.2、删除不必要的东西
3.3、定义Main.vue和Login.vue组件
Main.vue:
<template>
<h1>首页</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
Login.vue:
<template>
<div>
<el-card class="box-card">
<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>
</el-card>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
form: {
username: '',
password: ''
},
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) {
this.$router.push("/main");
} else {
this.dialogVisible = true;
return false;
}
});
}
}
}
</script>
<style scoped>
.box-card {
width: 480px;margin: auto;
}
</style>;
3.4、管理路由
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
}
]
});
3.5、在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)
})
3.6、在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>
3.7、测试
3.8、项目结构
|