sourcecode

vue 외부 기능, vue에 데이터 전달

copyscript 2022. 7. 31. 22:18
반응형

vue 외부 기능, vue에 데이터 전달

vue 컴포넌트 외부에서 실행되는 기능이 있습니다.반환되는 데이터를 vue 컴포넌트의 데이터로 전달합니다.

    <script>
      function example(){
        var item = 'item';
      };

      example();

      export default {
        data(){
          return (this is where I want item represented)
        }
      }

작업 데모 참조:

var app = new Vue({
  el: '#app',
  data: {
    item: "Hi"
  }
});


function example(){
      app.item='Hello How R You ?';
};
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>

<div id="app">
<button onclick="example()" >Click ME</button>
  {{ item }}
</div>

함수를 에 할당하다const컴포넌트 라이프 사이클 훅 중 하나로 호출합니다.

const example = function (){
    return 'item';
};

export default {
    created () {
        this.item = example()
    },
    data(){
      return {
         item: null
      }
    }
  }

언급URL : https://stackoverflow.com/questions/48804328/function-outside-vue-passing-data-into-vue

반응형