JavaScript初探_3

本文主要参考这里:参考地址,感谢博主。

前言

上一篇博客中通过“麻将”的实例已经简单了解了vue工程中helloword.vue的结构以及部分vue指令,那么从这篇博客开始,将进行一些小模块的实现,首先是登陆界面的制作。

开始

新建工程

新建工程已经讲过,这里需要说明一点的是,由于这里制作登陆界面,登陆跳转需要用到路由功能,也就是vue-router,为了后续不用再手动添加,所以在创建工程时候这一项就设为yes。

添加vue

在src/components目录下新建两个.vue文件,分别起名为Main.vue和Login.vue,分别用于制作主页面和跳转后的登陆页面。

修改index.js

在src/router目录下修改index.js文件,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Vue from 'vue'
import Router from 'vue-router'
//import HelloWorld from '@/components/HelloWorld'
import Main from '@/components/Main.vue'
import Login from '@/components/Login.vue'

Vue.use(Router) //注册vue-router

export default new Router({
routes: [
/*{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},*/
{
path: '/',
name: 'Main',
component: Main
},
{
path: '/login',
name: 'Login',
component: Login
},
]
})

主要是注销掉工程自带的helloworld相关内容,改为新建的Main和Login相关页面。这样路由就设置好了,再在main.js中引入路由即可(原main.js已引入,故这里无需更改)。

修改vue

接下来就是修改界面,其实也就是对三个.vue文件的操作,分别为:

App.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
<div id="app">
<img src="./assets/logo.png">
<router-view/>
</div>
</template>

<script>
export default {
name: 'App'
}
</script>

<style>
</style>

Main.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<template>
<div>
<h1>主页面</h1> 欢迎!<b @click="login">点这里登录</b>
</div>
</template>
<script>
export default {
methods: {
login() {
this.$router.replace('/login')
}
}
}
</script>

Login.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<template>
<div>
<h1>登录界面</h1> 用户名:<Input /><br /> 密码:<Input /><br /> <button @click="login">登录</button>
</div>
</template>
<script>
export default {
methods: {
login() {
this.$router.replace('/')
}
}
}
</script>

运行

保存运行之后可以看到如下界面:
主页面
点击黑色粗体字,可跳转到如下页面:
登陆界面
点击登陆按钮,可跳转回主页面,所以初步实现了页面的跳转。
注意一下两个页面的url,可以发现这个和index.js中的设置有关。

改进

实现了上述跳转功能,但是很明显,页面特点一个字:丑。
如何变好看呢?需要用到一个UI框架,Element。

安装Element

安装指导Element官网有说明文档,可以参考,但作者建的工程和官网稍有不同,故有些地方有所不同,请自行参考,并且element的使用教程官网也很详细,建议参考。

  • 在工程目录下cnpm安装,命令为
    npm i element-ui -S
  • 在src目录下新建element文件夹,文件夹内新建index.js文件,文件内代码引入:
1
2
3
4
5
6
//注意这里的代码和官网的有不同
import Vue from 'vue';
import ElementUI from 'element-ui';
import '../../node_modules/element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);
  • 在main.js引入
    import './element'

以上。

:转载文章请注明出处,谢谢~