How to pass data(json) to vue instance

前端 未结 4 1790
囚心锁ツ
囚心锁ツ 2020-12-31 23:53

I have a simple Vue instance and want to pass json from the backend to vue without HTTP request because it\'s always the same.

I\'ve tried do this with props, but it

4条回答
  •  庸人自扰
    2021-01-01 00:32

    I don't know if there is any Laravel helper for this but I will present a generic approach.

    One option would be to store you JSON data in a global variable and the page loads and then use it in your js files.

    Basically you need to generate some html similar to:

    
    

    Then from javascript you should be able to access the myApp.userData variable and use it when initializing the Vue component.

    new Vue({
        el: '#app',
        data: {
            userData: myApp.userData
        }
    });
    

    Here is an example:

    new Vue({
      el: '#app',
      data: {
        userData: myApp.userData
      }
    });
    
    
    
    
    Hello {{userData.firstName}}

提交回复
热议问题