简体   繁体   中英

Language in VueJS from JSON

I am trying to load locale language variables from a JSON Request (laravel generated) to VueJS since VueJS does not support locale out of the box. The ready function alert does not alert but the random text data variable does work. I know VueJS is loading correctly. There are no console errors and webpack compiles the vue. The lang array says empty and the lang.email shows blank. This is my issue. Any help appreciated.

const app = new Vue({
    el: '#app',
    data: {
      lang: [],
      randomtext: 'This is Random Text'
    },
    ready: function() {
      alert('THIS DOES NOT ALERT');
      this.getLanguage();
     },
     methods: {
       getLanguage: function() {
         this.$http.get('/lang/auth').then((response) => {
            this.$set("lang", response)
          }, (response) => {
            alert(response);
          });
       }
     }
});

the 'lang/auth'

{"email":"Email Address","password":"Password"}

my html:

 <h5 class="content-group">@{{ randomtext }}</h5> // This Works
 <input type="text" class="form-control" :placeholder="lang.email"> // This does not

Indeed, "ready" was deprecated in Vue.js 2

Try using " mounted " instead.

First , Change ready: into mounted:

(Because, vuejs version 2 doesn't support it anymore)

Second , Instead of using this.$set use this.lang = response

Here is the full code https://jsfiddle.net/uqp7f4zL/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM