Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用谷歌翻译api翻译外的方法?_Javascript_Vue.js_Google Translation Api - Fatal编程技术网

Javascript 如何使用谷歌翻译api翻译外的方法?

Javascript 如何使用谷歌翻译api翻译外的方法?,javascript,vue.js,google-translation-api,Javascript,Vue.js,Google Translation Api,我开始使用Cloud Google,并在代码中实现了转换API,但我不能在回调之外使用响应 方法:{ 点击(){ const text=“Olá”; const target=navigator.language; 翻译(文本,目标,函数(错误,翻译){ console.log(translation.translatedText) //this.newText=translation.translatedText; }); //console.log(this.newText); }, }使

我开始使用Cloud Google,并在代码中实现了转换API,但我不能在回调之外使用响应

方法:{
点击(){
const text=“Olá”;
const target=navigator.language;
翻译(文本,目标,函数(错误,翻译){
console.log(translation.translatedText)
//this.newText=translation.translatedText;
});
//console.log(this.newText);
},

}
使用function关键字更改“this”上下文。您可以将“this”保存在函数外部,也可以使用箭头函数

下面是如何使用箭头函数

methods:{
    clicked(){
        const text = "Olá";
        const target = navigator.language;
        googleTranslate.translate(text, target, (err, translation) => {
            console.log(translation.translatedText)
            //this.newText =  translation.translatedText;
        });
        //console.log(this.newText);
    },
}

非常感谢你。你能更好地解释函数和=>之间的区别吗?我不明白什么时候我需要使用一个或另一个箭头函数被认为更容易编写,因为您不必编写“function”或“return”,但它们是ES6功能,这意味着如果您以IE11为目标,则需要传输代码。大多数情况下,您可以毫无问题地使用箭头函数。我发现他善于解释。