<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Vuex Demo 01</title>
<script src="http://cdn.bootcss.com/vue/1.0.26/vue.min.js"></script>
<script src="http://cdn.bootcss.com/vuex/0.8.2/vuex.min.js"></script>
</head>
<body>
<!-- 2、view,映射到视图的数据counterValue; -->
<h3>Count is {{ counterValue }}</h3>
<div>
<button @click="increment">Increment +1</button>
<button @click="decrement">Decrement -1</button>
</div>
</body>
<script>
var app = new Vue({
el: 'body',
store: new Vuex.Store({
// 1、state,驱动应用的数据源;
state: {
count: 0
},
mutations: {
INCREMENT: function(state, amount) {
state.count = state.count + amount
},
DECREMENT: function(state, amount) {
state.count = state.count - amount
}
}
}),
vuex: {
getters: {
counterValue: function(state) {
return state.count
}
},
// 3、actions,响应在view上的用户输入导致的状态变化。
actions: {
increment: function({ dispatch, state }){
dispatch('INCREMENT', 1)
},
decrement: function({ dispatch, state }){
dispatch('DECREMENT', 1)
}
}
}
})
</script>
</html>
vue init webpack vue-notes-demo cd vue-notes-demo npm install // 安装依赖包 npm run dev // 启动服务
<template> <div class="jumbotron"> <h1>我的备忘录</h1> <p> <strong> <router-link to="/time-entries">创建一个计划</router-link> </strong> </p> </div> </template>
<template>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="text-center">所有计划的总时长: {{ time }} 小时</h3>
</div>
</div>
</template>
<script>
export default {
computed: {
time() {
return this.$store.state.totalTime
}
}
}
</script>
<template>
<div>
<router-link
v-if="$route.path !== '/time-entries/log-time'"
to="/time-entries/log-time"
class="btn create-plan">
创建
</router-link>
<div v-if="$route.path === '/time-entries/log-time'">
<h3>新的计划</h3>
</div>
<hr>
<router-view></router-view>
<div class="time-entries">
<p v-if="!plans.length"><strong>还没有任何计划(┬_┬),快去创建吧ヽ(●-`Д´-)ノ</strong></p>
<div class="list-group">
<a class="list-group-item" v-for="(plan,index) in plans">
<div class="row">
<div class="col-sm-2 user-details">
<img :src="plan.avatar" class="avatar img-circle img-responsive" />
<p class="text-center">
<strong>
{{ plan.name }}
</strong>
</p>
</div>
<div class="col-sm-2 text-center time-block">
<p class="list-group-item-text total-time">
<span class="glyphicon glyphicon-time">计划总时间:</span>
{{ plan.totalTime }}
</p>
<p class="label label-primary text-center">
<span class="glyphicon glyphicon-calendar">开始时间:</span>
{{ plan.date }}
</p>
</div>
<div class="col-sm-7 comment-section">
<p>备注信息:{{ plan.comment }}</p>
</div>
<button
class="btn btn-xs delete-button"
@click="deletePlan(index)">
X
</button>
</div>
</a>
</div>
</div>
</div>
</template>
<script>
export default {
name : 'TimeEntries',
computed : {
plans () {
return this.$store.state.list
}
},
methods : {
deletePlan(idx) {
// 减去总时间
this.$store.dispatch('decTotalTime',this.plans[idx].totalTime)
// 删除该计划
this.$store.dispatch('deletePlan',idx)
}
}
}
</script>
<template>
<div class="form-horizontal">
<div class="form-group">
<div class="col-sm-6">
<label>开始日期:</label>
<input
type="date"
class="form-control"
v-model="date"
placeholder="Date"
/>
</div>
<div class="col-sm-6">
<label>总时间 :</label>
<input
type="number"
class="form-control"
v-model="totalTime"
placeholder="Hours"
/>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<label>备注 :</label>
<input
type="text"
class="form-control"
v-model="comment"
placeholder="Comment"
/>
</div>
</div>
<button class="btn btn-primary" @click="save()">保存</button>
<router-link to="/time-entries" class="btn btn-danger">取消</router-link>
<hr>
</div>
</template>
<script>
export default {
name : 'LogTime',
data() {
return {
date : '',
totalTime : '',
comment : ''
}
},
methods:{
save() {
const plan = {
name : 'eraser',
image : 'https://pic.cnblogs.com/avatar/504457/20161108225210.png',
date : this.date,
totalTime : this.totalTime,
comment : this.comment
};
this.$store.dispatch('savePlan', plan)
this.$store.dispatch('addTotalTime', this.totalTime)
this.$router.go(-1)
}
}
}
</script>
import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'
Vue.use(Vuex);
const state = {
totalTime: 0,
list: []
};
export default new Vuex.Store({
state,
mutations,
actions
})
import * as types from './mutation-types'
export default {
addTotalTime({ commit }, time) {
commit(types.ADD_TOTAL_TIME, time)
},
decTotalTime({ commit }, time) {
commit(types.DEC_TOTAL_TIME, time)
},
savePlan({ commit }, plan) {
commit(types.SAVE_PLAN, plan);
},
deletePlan({ commit }, plan) {
commit(types.DELETE_PLAN, plan)
}
};
actions: {
increment ({ commit }) {
commit('increment')
}
}
import * as types from './mutation-types'
export default {
// 增加总时间
[types.ADD_TOTAL_TIME] (state, time) {
state.totalTime = state.totalTime + time
},
// 减少总时间
[types.DEC_TOTAL_TIME] (state, time) {
state.totalTime = state.totalTime - time
},
// 新增计划
[types.SAVE_PLAN] (state, plan) {
// 设置默认值,未来我们可以做登入直接读取昵称和头像
const avatar = 'https://pic.cnblogs.com/avatar/504457/20161108225210.png';
state.list.push(
Object.assign({ name: 'eraser', avatar: avatar }, plan)
)
},
// 删除某计划
[types.DELETE_PLAN] (state, idx) {
state.list.splice(idx, 1);
}
};
// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
mutations: {
// 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
[SOME_MUTATION] (state) {
// mutate state
}
}
// 增加总时间或者减少总时间 export const ADD_TOTAL_TIME = 'ADD_TOTAL_TIME'; export const DEC_TOTAL_TIME = 'DEC_TOTAL_TIME'; // 新增和删除一条计划 export const SAVE_PLAN = 'SAVE_PLAN'; export const DELETE_PLAN = 'DELETE_PLAN';
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue-notes-demo</title> </head> <body> <div id="app"> <router-view></router-view> </div> </body> </html>
import Vue from 'vue';
import App from './App';
import Home from './components/Home';
import TimeEntries from './components/TimeEntries.vue'
import VueRouter from 'vue-router';
import VueResource from 'vue-resource';
import store from './vuex/index';
// 路由模块和HTTP模块
Vue.use(VueResource);
Vue.use(VueRouter);
const routes = [
{ path: '/home', component: Home },
{
path : '/time-entries',
component : TimeEntries,
children : [{
path : 'log-time',
// 懒加载
component : resolve => require(['./components/LogTime.vue'],resolve),
}]
},
{ path: '*', component: Home }
]
const router = new VueRouter({
routes // short for routes: routes
});
// router.start(App, '#app');
const app = new Vue({
router,
store,
...App,
}).$mount('#app');
<!-- // src/App.vue -->
<template>
<div id="wrapper">
<nav class="navbar navbar-default">
<div class="container">
<a class="navbar-brand" href="#">
<i class="glyphicon glyphicon-time"></i>
备忘录
</a>
<ul class="nav navbar-nav">
<li><router-link to="/home">首页</router-link></li>
<li><router-link to="/time-entries">计划列表</router-link></li>
</ul>
</div>
</nav>
<div class="container">
<div class="col-sm-9">
<router-view></router-view>
</div>
<div class="col-sm-3">
<sidebar></sidebar>
</div>
</div>
</div>
</template>
<script>
import Sidebar from './components/Sidebar.vue'
export default {
components: { 'sidebar': Sidebar },
}
</script>
<style>
.router-link-active {
color: red;
}
body {
margin: 0px;
}
.navbar {
height: 60px;
line-height: 60px;
background: #333;
}
.navbar a {
text-decoration: none;
}
.navbar-brand {
display: inline-block;
margin-right: 20px;
width: 100px;
text-align: center;
font-size: 28px;
text-shadow: 0px 0px 0px #000;
color: #fff;
padding-left: 30px;
}
.avatar {
height: 75px;
margin: 0 auto;
margin-top: 10px;
/* margin-bottom: 10px; */
}
.text-center {
margin-top: 0px;
/* margin-bottom: 25px; */
}
.time-block {
/* padding: 10px; */
margin-top: 25px;
}
.comment-section {
/* padding: 20px; */
/* padding-bottom: 15px; */
}
.col-sm-9 {
float: right;
/* margin-right: 60px; */
width: 700px;
min-height: 200px;
background: #ffcccc;
padding: 60px;
}
.create-plan {
font-size: 26px;
color: #fff;
text-decoration: none;
display: inline-block;
width: 100px;
text-align: center;
height: 40px;
line-height: 40px;
background: #99cc99;
}
.col-sm-6 {
margin-top: 10px;
margin-bottom: 10px;
}
.col-sm-12 {
margin-bottom: 10px;
}
.btn-primary {
width: 80px;
text-align: center;
height: 30px;
line-height: 30px;
background: #99cc99;
border-radius: 4px;
border: none;
color: #fff;
float: left;
margin-right: 10px;
font-size: 14px;
}
.btn-danger {
display: inline-block;
font-size: 14px;
width: 80px;
text-align: center;
height: 30px;
line-height: 30px;
background: red;
border-radius: 4px;
text-decoration: none;
color: #fff;
margin-bottom: 6px;
}
.row {
padding-bottom: 20px;
border-bottom: 1px solid #333;
position: relative;
background: #f5f5f5;
padding: 10px;
/* padding-bottom: 0px; */
}
.delete-button {
position: absolute;
top: 10px;
right: 10px;
}
.panel-default {
position: absolute;
top: 140px;
right: 60px;
}
</style>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有