首先新建一個項目這個很多博客都講過,就不再贅述,下面直接開始做demo,一個小時搞定vue小demo。
新建文件夾view存放頁面,demo共有5個頁面,Add.vue、Home.vue、List.vue、Login.vue、User.vue。
懶得寫CSS代碼,用的Elementui,感興趣的小夥伴可以嘗試一下。
創建好文件之後開始寫代碼,擼起袖子加油幹!!!
打開main.js
// 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.use(ElementUI)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
打開router文件下的index.js文件,如下:
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/view/Login'
import Home from '@/view/Home'
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
linkActiveClass: 'active',
routes: [
{
path: '/',
name: 'login',
component: Login
},
{
path: '/home',
name: 'home',
component: Home,
children: [
{
path: 'list',
name: 'list',
component: () => import(/*webpackChunkName: "list" */ '@/view/List')
},
{
path: 'user',
name: 'user',
component: () => import(/*webpackChunkName: "user" */ '@/view/User')
}
]
},
{
path: '/add',
name: 'add',
component: () => import(/*webpackChunkName: "add" */ '@/view/Add')
}
]
})
在src文件下新建一個store.js文件,
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
lists: []
},
mutations: {
addItem (state, value) {
state.lists.push(value)
}
},
actions: {
}
})
打開App.vue
<template>
<div>
<router-view></router-view>
</div>
</template>
<script>
export default {
};
</script>
<style>
* {
padding: 0;
margin: 0;
}
h3{
text-align: center;
height: 60px;
line-height: 60px;
}
</style>
打開Login.vue,實現註冊登錄功能:
<template>
<div id="login">
<el-form label-width="25%" v-if="!isReg">
<h3>歡迎登錄</h3>
<el-form-item label="用戶名:">
<el-input type="text" v-model="name"></el-input>
</el-form-item>
<el-form-item label="密碼:">
<el-input type="password" v-model="password"></el-input>
</el-form-item>
<el-form-item>
<el-button class="butt" type="primary" @click="login()">登錄</el-button>
<el-button class="butt" @click="reg()">註冊</el-button>
</el-form-item>
</el-form>
<el-form label-width="25%" v-else>
<h3>請輸入以下信息</h3>
<el-form-item label="用戶名:">
<el-input type="text" v-model="name"></el-input>
</el-form-item>
<el-form-item label="密碼:">
<el-input type="password" v-model="password"></el-input>
</el-form-item>
<el-form-item label="確認密碼:">
<el-input type="password" v-model="repeat"></el-input>
</el-form-item>
<el-form-item>
<el-button class="butt" type="primary" @click="addUser()">確定</el-button>
<el-button class="butt" @click="cancel()">取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
isReg: false,
name: '',
password: '',
repeat: ''
}
},
methods: {
login () {
if(localStorage.getItem("name")===this.name && localStorage.getItem("password")===this.password) {
this.name = ''
this.password = ''
this.$router.push('/home/list')
} else {
alert('用戶名密碼不正確!')
}
},
reg () {
this.isReg = true
},
addUser () {
if(this.password === this.repeat) {
localStorage.setItem("name", this.name)
localStorage.setItem("password", this.password)
this.name = ''
this.password = ''
this.isReg = false
} else {
alert("兩次密碼輸入不一致!")
}
},
cancel () {
this.isReg = false
}
}
}
</script>
<style scoped>
#login{
margin: 10px;
}
.butt{
width: 30%;
}
</style>
打開Home.js
<template>
<div>
<router-view></router-view>
<ul class="footer">
<li class="icons"><router-link :to="{name: 'list'}">新聞列表</router-link></li>
<li class="icons"><router-link :to="{name: 'user'}">個人中心</router-link></li>
</ul>
</div>
</template>
<script>
export default {
name: "Home"
}
</script>
<style scoped lang="scss">
li{
list-style: none;
}
.footer{
position: fixed;
width: 100%;
height: 60px;
line-height: 60px;
left: 0;
bottom: 0;
display: flex;
flex-flow: row nowrap;
justify-content: space-around;
}
.icons{
font-size: 16px;
flex: 1;
text-align: center;
border-top: 1px solid #42b983;
a{
color: #42b983;
display: block;
text-decoration:none;
&.active{
color: #fff;
background: #42b983;
}
}
}
</style>
打開List.vue,實現新聞列表頁:
<template>
<div id="list">
<h3>新聞列表</h3>
<el-card class="box-card" v-for="(item, index) in pageLists" :key="index" shadow="always">
<span>{{item.title}}</span>
<div class="bottom clearfix">
<time class="text">{{item.content}}</time>
</div>
</el-card>
</div>
</template>
<script>
import store from '@/store'
export default {
name: "List",
store,
computed: {
pageLists() {
return store.state.lists
}
}
}
</script>
<style scoped>
#list, .box-card {
margin: 10px;
}
.text {
font-size: 13px;
color: #999;
}
</style>
打開Add.vue,實現新聞添加頁:
<template>
<div id="add">
<el-form label-width="25%">
<h3>添加新聞</h3>
<el-form-item label="標題:">
<el-input type="text" v-model="title"></el-input>
</el-form-item>
<el-form-item label="內容:">
<el-input type="text" v-model="content"></el-input>
</el-form-item>
<el-form-item>
<el-button class="btn" type="primary" @click="add()">添加</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
import store from '@/store'
export default {
name: "Add",
store,
data() {
return {
title: '',
content: ''
}
},
methods: {
add() {
store.commit('addItem', {
title: this.title,
content: this.content
})
this.title = ''
this.content = ''
this.$router.push('/home/list')
}
}
}
</script>
<style scoped>
#add{
margin: 10px;
}
</style>
打開User.vue,實現個人中心頁:
<template>
<div id="user">
<img src="@/assets/logo.png" alt="">
<p>{{this.name}}</p>
<p>歡迎來到個人中心</p>
<el-button class="addto" type="primary" @click="toAdd()">添加</el-button>
</div>
</template>
<script>
export default {
name: "user",
data(){
return {
name: localStorage.getItem("name")
}
},
methods: {
toAdd() {
this.$router.push('/add')
}
}
}
</script>
<style scoped>
#user{
text-align: center;
}
.addto{
width: 90%;
margin: 10px;
background-color: #42b983;
border-color: #42b983;
}
</style>
到此頁面實現完成!