1.過濾器:
filters: {
search(list) { es5
var _self = this;
//return list.filter(menu => menu.childs.name.join("").indexOf(this.searchVal) > -1);
return list.filter(function(menu) {
var lists = menu.childs;
var arr = [];
arr.push(menu.name)
for (var i = 0, len = lists.length; i < len; i++) {
arr.push(lists[i].name);
}
return arr.join("").indexOf(_self.searchVal) > -1;
})
},
searchlist(list) { es6
return list.filter(menulist => menulist.name.indexOf(this.searchVal) > -1);
}
},
2.ajax:
方法一:在寫ready里面
ready: function() {
var _self = this;
$.ajax({
url: 'url',
dataType: 'json',
success: function(data) {
_self.menus = data.data.menu.childs;
_self.user = data.data.user;
},
error: function(err) {
console.log(err);
}
})
},
3.用路由的時候又要傳數據(但是router.start和new Vue不能同時寫)
router.start({
data() {
return {
currentmenu: '',
message: 'asdaaa',
menus: [],
user: {},
searchVal: ''
}
},
filters: {
search(list) {
var _self = this;
//return list.filter(menu => menu.childs.name.join("").indexOf(this.searchVal) > -1);
return list.filter(function(menu) {
var lists = menu.childs;
var arr = [];
arr.push(menu.name)
for (var i = 0, len = lists.length; i < len; i++) {
arr.push(lists[i].name);
}
return arr.join("").indexOf(_self.searchVal) > -1;
})
},
searchlist(list) {
return list.filter(menulist => menulist.name.indexOf(this.searchVal) > -1);
}
},
ready: function() {
var _self = this;
// console.log(env.api)
$.ajax({
url: 'url',
dataType: 'json',
success: function(data) {
// console.log(data.data.menu.childs);
// console.log(data.data.user);
_self.menus = data.data.menu.childs;
_self.user = data.data.user;
},
error: function(err) {
console.log(err);
}
})
},
methods: {
changeCurrent: function(curl) {
// alert(this.message)
this.currentmenu = curl;
}
},
component: app
}, "#app");
4.文件上傳
$.ajaxFileUpload({
url: 'url',
type: 'post',
secureuri: false, //是否啟用安全提交,默認為false
fileElementId: 'imeiExcelUrl', //文件選擇框的id屬性
dataType: 'json', //返回數據的類型
success: function(data) { //服務器響應成功時的處理函數
config.data.show = false;
if (data.meta.code == '200') {
window.location.reload();
}
console.log(data.meta);
},
error: function(data) { //服務器響應失敗時的處理函數
console.log(data);
}
});
5.用v-if指令后,對該dom綁定事件不起作用
換了v-show
6.有沒有可以監聽地址欄變化的方法
還沒解決
7.復制文字到剪切板
(1)
<!-- 點擊復制begin -->
<script type="text/javascript" src="/js/zclip/jquery.zclip.js"></script>
<!-- 點擊復制end -->
(2)
$("#phone_number").zclip({
path: "/js/zclip/ZeroClipboard.swf",
copy: function(){
return $(this).val();
},
beforeCopy:function(){/* 按住鼠標時的操作 */
$(this).css("color","orange");
},
afterCopy:function(){/* 復制成功后的操作 */
var $copysuc = $("<div class='copy-tips'><div class='copy-tips-wrap'>☺ 復制成功</div></div>");
$("body").find(".copy-tips").remove().end().append($copysuc);
$(".copy-tips").fadeOut(3000);
}
});
8.文件上傳綁定的onchange事件,用的ajaxfileupload.js 但是change事件第二次失效。
用juqery.upload.js解決了。
$('#img').fileupload({
add: function(e, data) {
var uploadErrors = [];
var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
if (data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push('傳入的格式不對');
}
if (data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 5000000) {
uploadErrors.push('所選圖片太大');
}
if (uploadErrors.length > 0) {
alert(uploadErrors.join("\n"));
} else {
data.submit();
}
},
url: env.api + "liveman/panorama/panora/cover",
dataType: 'json',
autoUpload: true,
//成功
done: function(e, data) {
_self.flagImg = true;
_self.dataObject.coverUrl = data.result.data;
},
//失敗
fail: function(e, data) {
console.log(data);
}
});
9.form表單的提交不跳轉
html:
<form method="post" name="testForm" id="showDataForm" enctype="multipart/form-data" v-bind:action="url" >
<label for="video_file" class="labelText col-sm-4 text-right">視 頻</label>
<div class="col-sm-2">
<input type="file" id="video_file" name="video_file">
</div>
<div class="col-sm-1">
<button type="submit" class="submit" >提交</button>
</div>
</form>
js:
$("#showDataForm").on('submit',function(event) {
$("#showDataForm").ajaxSubmit(function(message) {
// 提交表單的返回值
// console.log(message);
if(message.code == 0) {
alert("視頻上傳成功");
}else {
alert('視頻上傳失敗');
}
});
return false; // 必須返回false,否則表單會自己再做一次提交操作,並且頁面跳轉
});
10.XMLHttpRequest上傳文件做進度條
//進度條函數
function videoUploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
_self.progressBar = percentComplete;
}
else {
console.log(".......")
}
}
//文件上傳成功函數
function videoUploadComplete(evt) {
var response = JSON.parse(evt.currentTarget.response);
if(response.code == 0){
_self.flagVideo = true;
alert("視頻上傳成功");
}
}
//文件上傳函數
function uploadVideo(url) {
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append("show_form", document.getElementById('video_file').files[0]);
xhr.upload.addEventListener("progress", function(){videoUploadProgress(event)}, false);
xhr.addEventListener("load", videoUploadComplete, false);
xhr.open("POST", url);
console.log(url);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(fd);
}
$("#submit").on('click',function(){
uploadVideo(_self.url);
});
11.綁定圖片
<img v-bind:src="item.image">
12.綁定value到Vue實例的一個動態屬性上
對於單選按鈕,勾選框及選擇框選項,
v-model綁定的value通常是靜態字符串(對於勾選框是邏輯值):
13.vue頁面閃爍問題{{message}}
樣式:
[v-cloak]{
display:none;
}
dom:
<div v-cloak>{{message}}</div>
14.vuejs中過渡動畫
樣式:
.staggered-transition {
transition: all .5s ease;
/*overflow: hidden;*/
margin: 0;
}
.staggered-enter,
.staggered-leave {
opacity: 0;
}
dom:
<li class=" treeview" v-for="menu in menus |search" v-bind:class="{'active': searchVal!=''||$index == activeMemuIndex}" transition="staggered" stagger="100">
15.局部樣式
<style scoped></style>
支持sass
<style scoped lang='sass'></style>
16.webpack區分測試環境、開發環境和正式環境
package.json:
"scripts": {
"start": "webpack-dev-server --inline",
"test": "webpack",
"build": "webpack"
},
webpack.config.js:
if (TARGET === 'start') {
module.exports.plugins = [
new webpack.DefinePlugin({
'env': {
api: '"kkk"' 測試環境
}
}),
new TransferWebpackPlugin([
{ from: 'exter' }
], path.resolve(__dirname, "src")),
new TransferWebpackPlugin([
{ from: 'login' }
], path.resolve(__dirname, "src"))
]
module.exports.devtool = '#source-map'
} else {
module.exports.plugins = [
new webpack.DefinePlugin({
'env': {
api: '"kkk"' 發布環境
}
}),
new TransferWebpackPlugin([ 文件拷貝
{ from: 'exter' }
], path.resolve(__dirname, "src")),
new TransferWebpackPlugin([
{ from: 'login' }
], path.resolve(__dirname, "src"))
]
module.exports.devtool = '#source-map'
}
17.window.open()新窗口
window.location=當前窗口
18.公用模態框組件(父子組件通信)
export default {
props: {
modal: {
title: '',
content: '',
flag: '',
id: '',
show: false,
num: ''
},
focusList: []
},
methods: {
refresh: function() {
this.deleteFocusSure();
},
deleteFocusSure: function(id) {//確定刪除
var _self = this;
$.ajax({
url: 'url',
data: {id: _self.modal.id},
success: function(data) {
if(data.meta.code == 200) {
for (let i = 0; i < _self.focusList.length; i++) {
if(_self.focusList[i].id == _self.modal.id) {
_self.focusList.splice(i,1);
_self.modal.show = false;
_self.$dispatch('focusList', _self.focusList); //通知
}
}
}
},
err: function(err) {
console.log(err);
}
});
},
},
events: { //通知
'refresh' () {
this.refresh();
}
},
}
html:
<modal-file :modal="modal" :focus-list="focusList"></modal-file>
js:
import modalFile from './modalFile.vue';
export default {
data() {
return {
focusList: [],
modal: {}
}
},
components: {
modalFile
},
methods: {
deleteFocus: function(id) {//刪除
this.modal = {
show: true,
title: '刪除',
content: '確定刪除該數據?',
flag: 1,
id: id
}
},
Preview: function(data,type,userid) {//預覽
var _self = this;
if(type == 3 ) {
$.ajax({
url: 'ur',
data: {liveid:data,type:3},
success: function(data) {
var activityId = data.data;
_self.modal = {
show: true,
title: "直播預覽",
flag: 2,
id: activityId,
}
}
});
}
}
},
events: { //刷新
// 分頁組件傳回的表格數據(這里即為服務器傳回的數據)
'data' (data) {
this.focusList = data.data.data;
console.log(this.focusList);
},
'focusList' (focusList) {//刪除后的刷新
this.focusList = focusList;
}
}
}
19.watch的使用
watch: {
'selectedType': 'getPlaceholder'
}
'selectedType':是data里面定義的變量;
'getPlaceholder':是methods里面定義的方法
20.vue的分頁組件 pageBage.vue
<!-- 表格分頁組件 -->
<template>
<nav class="boot-nav">
<ul class="pagination boot-page">
<li>
<a href="javascript:void(0)" @click="wholePrevClick()">
<span aria-hidden="true">第一頁</span>
</a>
</li>
<li>
<a href="javascript:void(0)" aria-label="Previous" @click="onPrevClick()">
<span aria-hidden="true">«</span>
</a>
</li>
<li v-for="page in pages" :class="activeNum === $index ? 'active' : ''">
<a href="javascript:void(0)" v-text="page" @click="onPageClick($index)"></a>
</li>
<li>
<a href="javascript:void(0)" aria-label="Next" @click="onNextClick()">
<span aria-hidden="true">»</span>
</a>
</li>
<li>
<a href="javascript:void(0)" @click="wholeNextClick()">
<span aria-hidden="true">最后一頁</span>
</a>
</li>
</ul>
<div class="page-total">
共 <span v-text="pageTotal"></span> 頁
</div>
</nav>
</template>
<script>
export default {
props: {
// 頁碼
pages: {
type: Array,
default: function () {
return [1]
}
},
// 每頁顯示個數
len: {
type: Number,
default: 10
},
// 表格數據(數組)
data: {
type: Array,
default: function () {
return []
}
},
// AJAX地址
url: {
type: String,
default: ''
},
//當前頁的字段
currentPage: {
type: String,
default: ''
},
totalPages: {
type: String,
default: ''
},
totalName: {
type: String,
default: ''
},
// 顯示頁數
pageLen: {
type: Number,
default: 10
},
// 總頁數
pageTotal: {
type: Number,
default: 1
},
// 參數內容
param: {
type: Object,
default: function () {
return {}
}
},
//method
method:{
type:String,
default:'get'
}
},
data () {
return {
activeNum: 0,
first: -1,
last: -1
}
},
methods: {
// 第一頁
wholePrevClick: function() {
this.first = 1;
var newPages = [];
for (let i = 0; i < this.pages.length; i++) {
newPages[i] = i +1;
}
this.pages = newPages;
this.activeNum = 0;
this.getData();
},
// 最后一頁
wholeNextClick: function() {
this.last = this.pageTotal;
var newPages = [];
for (let i = 0; i < this.pages.length; i++) {
newPages[this.pages.length-i-1] = this.pageTotal-i;
}
this.pages = newPages;
this.activeNum = this.pages.length-1;
this.getData();
},
pageMake: function(index) {
let newPages = [];
let len2 = parseInt(this.pages.length/2);
if(this.pages[index] <= len2 || this.pageTotal <= this.pageLen || this.pages[index] > this.pageTotal-len2) {
for (let i = 0; i < this.pages.length; i++) {
newPages[i] = this.pages[i];
}
this.activeNum = index;
}else if( this.pages[index] <= this.pageTotal-len2) {
for (let i = 0; i < this.pages.length; i++) {
newPages[i] = this.pages[index]-len2+i;
}
this.activeNum = len2;
}
this.pages = newPages;
this.getData();
},
// 點擊頁碼刷新數據
onPageClick (index) {
this.pageMake(index);
},
// 上一頁
onPrevClick () {
// 當前頁是否為當前最小頁碼
if (this.activeNum > 0) {
// this.activeNum = this.activeNum - 1;
let index = this.activeNum -1;
this.pageMake(index);
} else {
if (this.pages[0] !== 1) {
let newPages = []
for (let i = 0; i < this.pages.length; i++) {
newPages[i] = this.pages[i] - 1
}
this.pages = newPages
this.getData()
}
}
},
// 下一頁
onNextClick () {
// 當前頁是否為當前最大頁碼
if (this.activeNum < this.pages.length - 1) {
// this.activeNum = this.activeNum + 1
let index = this.activeNum + 1;
this.pageMake(index);
} else {
if (this.pages[this.pages.length - 1] < this.pageTotal) {
let newPages = []
for (let i = 0; i < this.pages.length; i++) {
newPages[i] = this.pages[i] + 1
}
this.pages = newPages
this.getData()
}
}
},
// 獲取頁碼
getPages () {
this.pages = []
// 比較總頁碼和顯示頁數
if (this.pageTotal <= this.pageLen) {
//console.log(this.pageTotal+"....."+this.pageLen)
for (let i = 1; i <= this.pageTotal; i++) {
this.pages.push(i)
}
} else {
for (let i = 1; i <= this.pageLen; i++) {
this.pages.push(i)
}
}
},
// 頁碼變化獲取數據
getData () {
var _self = this;
this.param[_self.currentPage] = this.pages[_self.activeNum];
this.param.rows = this.len;
if( this.first!= -1) {
this.param[_self.currentPage] = this.first;
this.first = -1;
}else if (this.last != -1) {
this.param[_self.currentPage] = this.last;
this.last = -1;
}
$.ajax({
url: this.url,
method: this.method,
data: this.param,
dataType: 'json',
success: function(data) {
if(_self.totalName) {
_self.pageTotal = Math.ceil(data.data[_self.totalName][_self.totalPages]/ _self.len);
}else {
_self.pageTotal = Math.ceil(data.data[_self.totalPages]/ _self.len);
}
if (_self.pages.length !== _self.pageLen || _self.pageTotal < _self.pageLen) {
_self.getPages();
}
_self.$dispatch('data', data)
},
error: function(err) {
// console.log(err);
}
});
},
refresh () {
this.pages = [1]
this.activeNum = 0
this.getData()
}
},
ready () {
this.getData();
this.getPages();
},
events: {
'refresh' () {
this.refresh();
}
}
}
</script>
<style scoped>
.boot-select {
float: left;
width: 80px;
}
.boot-nav {
float: right;
}
.boot-page {
display: inline-block;
margin: 2px 10px 0 20px;
vertical-align: middle;
}
.page-total {
display: inline-block;
vertical-align: middle;
}
</style>
21.watch
定義了一個對象類型的數據(例如:obj),改變了屬性(例如:obj.name),觸發不了watch事件。
22.img
dom:
< img :src="img"/>
js:
data() {
return {
img: require('../../login/login/imgs/progress.gif')
}
}
23.vue-resource:
http://www.doc00.com/doc/1001004eg
24.子組件的索引:
1. 直接用$children索引,
this.$children[index]
2. 為組件添加索引ID后通過id獲取:
template:
<live-player v-ref:liverplayer :live-title="liveTitle" :live-src="liveSrc"></live-player>
script:
this.$refs.liverplayer
注意:如果v-ref:liverplayerM再用this.$refs.liverplayerM會取不到值(因為用了駝峰法吧)。
25.設置了disabled的a標簽置灰但是@click的點擊仍然可用
