溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue translate peoject實現(xiàn)在線翻譯功能【新手必看】

發(fā)布時間:2020-08-20 17:44:19 來源:腳本之家 閱讀:217 作者:ZONE_98F 欄目:web開發(fā)

 開始

這是一適合新手練習(xí)的小項目,一個在線翻譯的demo。

vue translate peoject實現(xiàn)在線翻譯功能【新手必看】

在正式開始前,先啰嗦一下,是一位網(wǎng)友給我的建議,就是不要強(qiáng)行組件化的問題 開始做Vue時我們可能會喜歡拆很多組件出來 但記住組件是為了復(fù)用(常見如公共菜單按鈕欄等) 如非能夠復(fù)用的情況其實并不用真的拆出組件來 。

當(dāng)然,這個項目里因為是練手,所以強(qiáng)行組件化來涉及更多的vue用法。

目錄結(jié)構(gòu)

src下新建了兩個文件:TranslateForm.vue表單組件和TranslateText.vue翻譯結(jié)果組件

vue translate peoject實現(xiàn)在線翻譯功能【新手必看】

涉及的語法

  • 指令:v-model,v-on,v-bind
  • 父子組件通信:$emit,props
  • 動態(tài)更新數(shù)據(jù):vm.$set
  • 翻譯服務(wù)的API,我這里用的是有道翻譯的api

——————————————————————————————分割線————————————————————————

TranslateForm.vue
<template>
 <div>
 <!--加上頁面修飾符,提交時就不回再重載頁面-->
 <form v-on:submit.prevent="formSubmit">
  <input type="text" v-model="text" placeholder="輸入需要翻譯的內(nèi)容"/>
  <select v-model="to">
  <option value ="en">英文</option>
  <option value ="ko">韓文</option>
  <option value ="fr">法文</option>
  <option value ="ru">俄文</option>
  </select>
  <input type="submit" value="翻譯"/>
 </form>
 </div>
</template>
<script>
export default {
 name: 'TranslateForm',
 data: function () {
 return {
  text: '',
  to: 'en'
 }
 },
 methods: {
 formSubmit: function () {
  this.$emit('formSubmit', this.text, this.to)
 }
 }
}
</script>
<style></style>

這里沒啥好說的,text和to兩個變量分別是要翻譯的文字和翻譯語言的選項,this.$emit把數(shù)據(jù)傳給父組件使用

根組件APP

<template>
 <div id="app">
 <h3>簡單翻譯</h3><span>簡單/易用/便捷</span>
 <TranslateForm v-on:formSubmit="textTranslate"></TranslateForm>
 <TranslateText :translated-text="translatedText"></TranslateText>
 </div>
</template>
<script>
import TranslateForm from './components/TranslateForm.vue'
import TranslateText from './components/TranslateText.vue'
import md5 from 'blueimp-md5'
import $ from 'jquery'

export default {
 name: 'App',
 data: function () {
 return {
  translatedText: '2',
  appKey: '47bb6e424790df89',
  key: 'NH2VxBadIlKlT2b2qjxaSu221dSC78Ew',
  salt: (new Date()).getTime(),
  from: '',
  to: 'en'
 }
 },
 components: {
 TranslateForm, TranslateText
 },
 methods: {
 textTranslate: function (text, to) {
  let vm = this
  $.ajax({
  url: 'http://openapi.youdao.com/api',
  type: 'post',
  dataType: 'jsonp',
  data: {
   q: text,
   appKey: this.appKey,
   salt: this.salt,
   from: this.from,
   to: to,
   sign: md5(this.appKey + text + this.salt + this.key)
  },
  success: function (data) {
   vm.$set(vm.$data, 'translatedText', data.translation[0])
  }
  })
 }
 }
}
</script>
<style>
 #app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
 }
</style>

1、父組件拿到子組件傳來的數(shù)據(jù)后開始通過api來請求數(shù)據(jù)
2、我用的是有道api http://ai.youdao.com/login.s api文檔里對于api的使用已經(jīng)很詳細(xì)了,我在這里是第一次使用api,沒覺得難
3、需要自己安裝兩個依賴:一個是jquery由于ajax請求api,一個是blueimp-md5在請求api時會用到里面的md5()
4、用vue.set將得到的結(jié)果綁定到translatedText這個變量,在這一步的時候我踩了兩個坑

第一個坑:習(xí)慣了以前的寫法,直接就這樣給變量賦值,結(jié)果變量的值并未改變,這時我還不知到有Vue.set這個語法,后面百度才知道的(不認(rèn)真看文檔的下場)

 success: function (data) {
   this.translatedText = data.translation[0]
   console.log(this.translatedText)
  }

第二個坑:照著文檔來寫,然后報錯了:this.$set is not a function,這里報錯是因為success這個函數(shù)里的this指向的不是當(dāng)前的VueModel     

success: function (data) {
   this.$set(this.$data, 'translatedText', data.translation[0])
  }

所以我在前面定義了一個vm變量來充當(dāng)當(dāng)前Model,然后就不報錯了。

TranslateText.vue
<template>
 <div id="TranslateText">
 <p>{{translatedText}}</p>
 </div>
</template>
<script>
export default {
 name: 'TranslateText',
 props: [
 'translatedText'
 ]
}
</script>
<style></style>

props接收父組件傳值來使用

最后

這個文章我自己看了一下,寫的確實不好,許多地方不通順,希望大家多多包涵

代碼我上傳到github了 https://github.com/Zone-F/vue... (沒加樣式)

以上所述是小編給大家介紹的vue translate peoject實現(xiàn)在線翻譯功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI