1、准备
#1.1 创建组件并配置路由
1、创建 src/views/login/index.vue
并写入以下内容
<template>
<div class="login-container">登录页面</div>
</template>
<script>
export default {
name: 'LoginPage',
components: {},
props: {},
data () {
return {}
},
computed: {},
watch: {},
created () {},
mounted () {},
methods: {}
}
</script>
<style scoped lang="less"></style>
2、然后在 src/router/index.js
中配置登录页的路由表
{
path: '/login',
name: 'login',
component: () => import('@/views/login')
}
最后,访问 /login
查看是否能访问到登录页面。
#1.2 布局结构
这里主要使用到三个 Vant 组件:
一个经验:使用组件库中的现有组件快速布局,再慢慢调整细节,效率更高(刚开始可能会感觉有点麻烦,越用越熟,慢慢的就有了自己的思想)。
src/views/login/index.vue
<template>
<div class="login-container">
<!-- 导航栏 -->
<van-nav-bar class="page-nav-bar" title="登录" />
<!-- /导航栏 -->
<!-- 登录表单 -->
<van-form @submit="onSubmit">
<van-field
name="用户名"
placeholder="请输入手机号"
/>
<van-field
name="验证码"
placeholder="请输入验证码"
/>
<div class="login-btn-wrap">
<!--其实van-button按钮如果包裹在van-form里面默认类型就是native-type,这里不加也可以-->
<van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
</div>
</van-form>
<!-- /登录表单 -->
</div>
</template>
<script>
export default {
name: 'LoginIndex',
components: {},
props: {},
data () {
return {
}
},
computed: {},
watch: {},
created () {},
mounted () {},
methods: {
onSubmit (values) {
console.log('submit', values)
}
}
}
</script>
<style scoped lang="less"></style>
#1.3 布局样式
写样式的原则:将公共样式写到全局(
src/styles/index.less
),将局部样式写到组件内部。
1、src/styles/index.less
body {
background-color: #f5f7f9;
}
.page-nav-bar {
background-color: #3296fa!important;
.van-nav-bar__title {
color: #fff;
}
.van-icon{
color: #fff!important;
}
}
2、src/views/login/index.vue
<template>
<div class="login-container">
<!-- 导航栏 -->
<van-nav-bar class="page-nav-bar" title="登录" />
<!-- /导航栏 -->
<!-- 登录表单 -->
<van-form @submit="onSubmit">
<van-field
name="用户名"
placeholder="请输入手机号"
>
<!--【增加图标】-->
<template #left-icon>
<i class="toutiao toutiao-shouji"></i>
</template>
</van-field>
<van-field
name="验证码"
placeholder="请输入验证码"
>
<!--【增加图标】-->
<template #left-icon>
<i class="toutiao toutiao-yanzhengma"></i>
</template>
<!--【增加发送按钮】-->
<template #button>
<!--这里注意,要加上native-type,否则也可以实现表单提交-->
<van-button class="send-sms-btn" round size="small" native-type="button" type="default">
发送验证码
</van-button>
</template>
</van-field>
<div class="login-btn-wrap">
<van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
</div>
</van-form>
<!-- /登录表单 -->
</div>
</template>
<script>
export default {
name: 'LoginIndex',
components: {},
props: {},
data () {
return {
}
},
computed: {},
watch: {},
created () {},
mounted () {},
methods: {
onSubmit (values) {
console.log('submit', values)
}
}
}
</script>
<style scoped lang="less">
.login-container {
.toutiao {
fon