npm install vue bootstrap
touch index.html app.js
<!-- index.html --><!doctype html><html><head> <meta charset="utf-8"> <title>Vue</title> <!-- CSS --> <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"></head><body> <!— 导航栏 --> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand"><i class="glyphicon glyphicon-bullhorn"></i> Vue 备忘录</a> </div> </nav> <!—应用的主要部分 --> <div class="container" id="events"> <!—添加一个表单 --> <div class="col-sm-6"> <div class="panel panel-default"> <div class="panel-heading"> <h3>Add an Event</h3> </div> <div class="panel-body"> </div> </div> </div> <!—展示备忘录的内容 --> <div class="col-sm-6"> <div class="list-group"> </div> </div> </div> <!-- JS --> <script src="node_modules/vue/dist/vue.js"></script> <script src="app.js"></script></body></html>
// app.jsnew Vue({// 和ID为”events”的容器绑定 el: '#events'});
// app.js new Vue({ //和ID为”events”的容器绑定 el: '#events', // 在data属性中我们定义和网页交互的数据 data: {}, // 在应用加载的时候会执行ready属性中的函数 ready: function() {}, // 我们在应用中使用到的方法会在methods属性中定义 methods: {}});
<div class="panel-body"> <div class="form-group"> <input class="form-control" placeholder="事件名称" v-model="event.name"> </div> <div class="form-group"> <textarea class="form-control" placeholder="事件描述" v-model="event.description"></textarea> </div> <div class="form-group"> <input type="date" class="form-control" placeholder="Date" v-model="event.date"> </div> <button class="btn btn-primary" @click="addEvent">提交</button></div>
// app.js ... data: { event: { name: '', description: '', date: '' }, events: []},// 在应用加载的时候会执行ready属性中的函数ready: function() { // 在应用加载的时候我们需要初始化数据 this.fetchEvents();},// 我们在应用中使用到的方法会在methods属性中定义methods: { // 我们定义一个方法来获取数据 fetchEvents: function() { var events = [{ id: 1, name: '开会', description: '上午9点在21层大会议室', date: '2015-09-10' }, { id: 2, name: '购物', description: '买个充电宝', date: '2015-10-02' }, { id: 3, name: '学习', description: '学习Vue官网上的教程', date: '2016-03-11' } ];// $set 是Vue提供的一个向数组插入数据的方法,在执行它的时候还会刷新视图 this.$set('events', events); }, //向事项数组插入数据 addEvent: function() { if(this.event.name) { this.events.push(this.event); this.event = { name: '', description: '', date: '' }; } }}
<div class="list-group"> <a href="#" class="list-group-item" v-for="event in events"> <h4 class="list-group-item-heading"> <i class="glyphicon glyphicon-bullhorn"></i> {{ event.name }} </h4> <h5> <i class="glyphicon glyphicon-calendar" v-if="event.date"></i> {{ event.date }} </h5> <p class="list-group-item-text" v-if="event.description">{{ event.description }}</p> <button class="btn btn-xs btn-danger" @click="deleteEvent($index)">Delete</button> </a></div>
deleteEvent: function (index) { if (confirm("Are you sure you want to delete this event?")) { // $remove is a Vue convenience method similar to splice this.events.$remove(index); } }
fetchEvents:function(){ this.$http.get('api/events').then(function(events) { this.$set('events', events); }).catch(function(error) { console.log(error); });}
addEvent: function(){ this.$http.post('api/events', this.event) .then(function (response) { this.events.push(this.event); console.log("Event added!"); }) .catch(function (error) { console.log(error); });}
deleteEvent:function(index){ this.$http.delete('api/events/' + event.id) .then(function (response) { this.events.$remove(index); }) .catch(function (error) { console.log(error); });}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有