安装依赖
- element-ui
npm i element-ui -S
配置
import ElementUI from 'element-ui'
Vue.use(ElementUI)
页面编写
<template>
<div>
<el-form :model="formData" ref="formData" :rules="rules">
<el-form-item label="用户名" prop="username">
<el-input v-model="formData.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="formData.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('formData')">
登录
</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: 'Login',
data () {
return {
formData: {
username: '',
password: ''
},
rules: {
username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
password: [{required: true, message: '请输入密码', trigger: 'blur'}]
}
}
},
methods: {
submitForm (formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
console.log('go to login')
} else {
console.log('validate failed')
}
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang='scss' scoped>
$backgroud-color: #ffff;
div {
background: $backgroud-color;
}
</style>
<template>
<div id="app">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Login',
component: Login
}
]
})
使用 scss 编写样式
npm install sass-loader --save-dev
npm install node-sass --sava-dev
<style lang='scss'>
$backgroud-color : #ffff;
div { color : $color ;}
</style>
问题
如果引入 scss 时遇到如下问题
TypeError: this.getOptions is not a function
// 卸载
npm uninstall --save less-loader
// 安装
npm install -D sass-loader@7.x
Node Sass version 5.0.0 is incompatible with ^4.0.0.
- 原因: node-sass 为v5.0.0,而sass-loader期望值为^ 4.0.0
- 解决方案:
// 卸载node-sass
npm uninstall node-sass
// 然后安装4.x版本(5.0之前)
npm install node-sass@4.14.1