日韩精品欧美激情国产一区_中文无码精品一区二区三区在线_岛国毛片AV在线无码不卡_亞洲歐美日韓精品在線_使劲操好爽好粗视频在线播放_日韩一区欧美二区_八戒八戒网影院在线观看神马_亚洲怡红院在线色网_av无码不卡亚洲电影_国产麻豆媒体MDX

vue定義全局變量和全局方法

時(shí)間:2019-09-08 09:32:33 類型:vue
字號(hào):    

一、全局引入文件

1、先定義共用組件 common.vue

    

<script type="text/javascript">
    // 定義一些公共的屬性和方法
    const baseUrl = 'http://tjegd.cn/'
    function getMyname() {
        console.log("南昌雅騰")
    }
    // 暴露出這些屬性和方法
    export default {
        baseUrl,
        getMyname
    }
</script>
2.  直接在單個(gè).vue文件中使用

<script lang="ts">
import { Component, Vue ,Watch } from 'vue-property-decorator';
import Nav from './Nav.vue';
import globals from '../components/common.vue'
@Component({
  components: {
    Nav,
  },
})

export default class Home extends Vue {
	 title:string = "南昌雅騰首頁(yè)";
	 content:string="";
	 classid:number=0;
	 mounted(){
	 	   console.log(globals.baseUrl);
           $("title").html(this.title);
      }
     created(){
     	
     }
}
</script>

二、main.js/main.tsyywr中引入全局變量和方法

     1. 定義common.vue同上

    2. main.ts文件中引入并賦值

import globals from './components/common'
Vue.prototype.globals = globals
  3. 直接在.vue文件中引用
export default class Home extends Vue {
	 title:string = "南昌雅騰首頁(yè)";
	 content:string="";
	 classid:number=0;
	 mounted(){
	 	   console.log(this.globals.baseUrl);
           $("title").html(this.title);
      }
     created(){
     	
     }
}