参考:https://juejin.im/post/59eddbfe6fb9a0450908abb4

定义

global.js

  1. let name = '小明'
  2. exports.install = function (Vue, options) {
  3. Vue.prototype.name = name
  4. Vue.prototype.text1 = function () {
  5. alert('执行成功1')
  6. }
  7. Vue.prototype.text2 = function () {
  8. alert('执行成功2')
  9. }
  10. }

引用

main.js

  1. import Global from './components/global'
  2. Vue.use(Global)

vue页面使用

  1. <template>
  2. <div id="app">
  3. {{name}}
  4. <router-view/>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'App',
  10. data () {
  11. return {
  12. name: this.name
  13. }
  14. },
  15. mounted () {
  16. this.text1()
  17. }
  18. }
  19. </script>