1 回答

TA貢獻1829條經驗 獲得超6個贊
代替:
fetchPartial() {
? ? ? ? fetch(this._partial).then(function (response) {
? ? ? ? ? ? // The API call was successful!
? ? ? ? ? ? return response.text();
? ? ? ? }).then(function (html) {
? ? ? ? ? ? let elem = document.querySelector( this._target ) // error: TypeError: undefined is not an object (evaluating 'this._target')
? ? ? ? ? ? elem.innerHTML = html
? ? ? ? }).catch(function (err) {
? ? ? ? ? ? // There was an error
? ? ? ? ? ? console.warn('Something went wrong.', err);
? ? ? ? });
? ? }
你應該做:
fetchPartial() {
? ? ? ? fetch(this._partial).then(function (response) {
? ? ? ? ? ? // The API call was successful!
? ? ? ? ? ? return response.text();
? ? ? ? }).then((html) => {
? ? ? ? ? ? let elem = document.querySelector( this._target ) // error: TypeError: undefined is not an object (evaluating 'this._target')
? ? ? ? ? ? elem.innerHTML = html
? ? ? ? }).catch(function (err) {
? ? ? ? ? ? // There was an error
? ? ? ? ? ? console.warn('Something went wrong.', err);
? ? ? ? });
? ? }
使用箭頭函數
添加回答
舉報