文章加密

;

2019年4月30日 星期二

疑問 Vue updated的觸發點

new Vue({
el:'#app',
data:{
msg:'请填写文檔内容'
},
beforeCreate(){
var vm=this;
$.ajax({
method:'post',
url:'',
data:'',
success(a){
vm.msg=a;
}
})
},
updated: function () {
console.log('a') //這個什麼時候會觸發呢?直接從html修改{{msg}}的內容並沒有觸發,或許創建個method修改msg觸發method時??
this.$nextTick(function () {
$("textarea").trigger('click');
})
}
})

click function not working after ajax →.on 是一種委託事件

当用ajax载入后端资料,並用js重新繪製table及其內的button後,原本放在$(funtion(){....})下的jQuery click function 就不執行了

The problem is that .click only works for elements already on the page. You have to use something like on if you are wiring up future elements.


所以應該把

$('.del').click(function(){
    alert("刪除");
});
改成

$(document).on("click",".del", function(){
  alert("刪除");
});

.on 是一種委託事件(delegate event),Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

For best performance, attach delegated events at a document location as close as possible to the target elements. Avoid excessive use of document or document.body for delegated events on large documents.

On a side note in the earlier versions of jQuery instead of .on() we used .live() or .delegate() functions.
參考: https://aiocollective.com/blog/click-doesn-t-work-after-ajax-load-jquery/

2019年4月24日 星期三

promise 及 Vue访问本地json时会出现TypeError: Cannot read property '0' of undefined

https://www.cnblogs.com/zhuzhenwei918/p/6915451.html   vue中promise的使用

https://segmentfault.com/a/1190000016680014   vue+axios+promise实际开发用法

https://www.youtube.com/watch?v=s6SH72uAn3Q

前因(問題)
Vue访问本地json时会出现TypeError: Cannot read property '0' of undefined


正解
在JSON请求回来的时候已经渲染页面了,此时你给的page3的默认值是[],所以page3.page3_aundefined,你再使用[0]当然就会报错了,你需要在初始化data的时候,把需要的结构都初始化,代码如下:
data: function() {
    return {
        page3: {
            page3_a: [{
                title: []
            }]
        }
    }
}
这样在数据没有回来的时候访问page3.page3_a[0].title才不会报错。



或是

index_content:{
                    announce:[{
                        info:''
                    }],
                    footer_image:[{
                        info:''
                    }],
                    footer_text:[{
                        info:''
                    }],
                    ftd_text:[{
                        info:''
                    }],
                    image:[{
                        info:''
                    },{
                        info:''
                    },{
                        info:'' //就是這個啦!
                    }],
                }

這樣index_content.image[2].info就不會出錯誤訊息!


※結果搜出promise順便了解下
可以快速理解的例子
const promise = new Promise((resolve, reject) => {
setTimeout(() =>{
const r =Math.random();
if(r >0.5){
resolve(r)
}else{
reject(r)
}
},1000)
})

promise.then((value) => {
console.log('成功',value)
}).catch((error) => {
console.log('失敗', error)
})


基础篇
let promiseToCleanTheRoom = new Promise(function(resolve, reject) {

  //cleaning the room

  let isClean = false;

  if (isClean) {
    resolve('Clean');
  } else {
    reject('not Clean');
  }

});

promiseToCleanTheRoom.then(function(fromResolve) {
  console.log('the room is' + fromResolve);
}).catch(function(fromReject){
console.log('the room is' + fromReject);
})


//结果:the room is not Clean


复杂篇
let cleanRoom = function() {
  return new Promise(function(resolve, reject) {
    resolve('Cleaned The Room');
  });
};

let removeGarbage = function(message) {
  return new Promise(function(resolve, reject) {
    resolve(message + ' remove Garbage');
  });
};

let winIcecream = function(message) {
  return new Promise(function(resolve, reject) {
    resolve( message + ' won Icecream');
  });
};

cleanRoom().then(function(result){
return removeGarbage(result);
}).then(function(result){
return winIcecream(result);
}).then(function(result){
console.log('finished ' + result);
})

//结果:finished Cleaned The Room remove Garbage won Icecream

进阶篇
阵列里全部执行完才触发
Promise.all ( [ cleanRoom(), removeGarbage(), winIcecream() ] ).then(funtion(){
    console.log('all finished!')
})

阵列里只要有一个执行完就触发(例如接收同样的data,只要拿到一个就可以往后执行)
Promise.race ( [ cleanRoom(), removeGarbage(), winIcecream() ] ).then(funtion(){
    console.log('one of them is finished')
})

2019年4月15日 星期一

背景執行不用session

session是屬於apache的語言

背景執行exec則屬於php的語言,php去讀了session但背景執行與apache無關,所以背景執行無法用session去寫。

(但是paul的可以,又但是我的不行)

2019年4月10日 星期三

之所以帳號不用中文

因為許功蓋問題,
即因為中文轉成編碼時,big5不支援部分(如許功蓋)文字,所以造成結果錯誤,
其他unicode可能有他們自己不支援的文字,如Jerry他們遇到的豆豆(不確定是哪個豆)問題~
因此帳號不用中文,但聽說有再慢慢修正