鍍金池/ 問答/HTML/ elementUi中hint定義為全局方法的問題

elementUi中hint定義為全局方法的問題

在index.js中定義了elementUI的提示框想全局使用;在組件中inport了,但是使用會報錯。不知道怎么解決this的問題
圖片描述

圖片描述

回答
編輯回答
柚稚

let hint = function(message, type) => {console.log(this)}
普通函數(shù)
以函數(shù)調(diào)用模式,直接調(diào)用 hint() ,
非嚴格模式下,里面的this指向全局變量,也就是里面的 vue對象
嚴格模式下,this指向undefined.
并且, ES6 的模塊自動采用嚴格模式,不管有沒有在模塊頭部加上"use strict";
箭頭函數(shù)
默認指向在定義它時所處的對象(宿主對象),不會指向vue.

所有 改成

 Vue.use(ElementUI);
 var vue = new Vue();
 vue.$message({})

就最直接了

2017年5月20日 03:12
編輯回答
互擼娃

箭頭函數(shù)

export let hint = (message, type) => {
    this.$message({
        message,
        type,
        customClass: 'hint',
        duration: 2000
    })
}
2017年11月9日 23:41