簡體   English   中英

如何從 Vue.js 文件中的 main.js 調用 Vue 方法

[英]How to call a Vue method from main.js in Vue.js file

為什么我無法在 App.vue 中調用我的方法? 應用程序中是否沒有 div id='App' 引用應該可以在其中調用方法?

主.js

new Vue({
  render: h => h(App),
  methods:{
    gesamt:function () {
       return 'Hello';
    }
  }
}).$mount('#app')

應用程序.vue

<template>
    <div id="app">
        <p>{{gesamt()}}</p>
    <navbar></navbar>
    <ausgaben></ausgaben>
    </div>
</template>


<script>
    export default {
        name:"App",
    }

</script>

如果你真的想為你的 VueJs 應用程序添加一個“全局”方法,你可以像這樣將它添加到 Vue 本身:

import {logEngagement} from "./helpers/logEngagement"
Vue.prototype.$logEngagement = logEngagement;

你可以像這樣在 component.vue 中使用它:

<script>
  export default {
    mounted() {
      this.$logEngagement.log("this");
    }
  }
<script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM