LoginSignup
43
37

More than 5 years have passed since last update.

Vue.use( )を書く場合と書かない場合

Last updated at Posted at 2018-07-26

 Vue.use( )って何よ :thinking:

  • Vue.jsのライブラリを使用する関数という理解 ( 個人的 )

※ 公式ドキュメント
https://jp.vuejs.org/v2/guide/plugins.html

Vue.useを書く場合

import Vue from 'vue/dist/vue.esm.js'
import VueRouter from 'vue-router'
import Index from '../components/index.vue'
import About from '../components/about.vue'
import Contact from '../components/contact.vue'

Vue.use(VueRouter)

export default new VueRouter({
  mode: 'history',
  routes: [
      { path: '/', component: Index },
      { path: '/about', component: About },
      { path: '/contact', component: Contact },
  ],
})
  • 上記のように Vue.jsのライブラリを使う場合 (VueRouter,Vuelidateなどなど)はVue.use(ライブラリ名)で使用するライブラリを読み込む

Vue.useを書かない場合

import axios from 'axios';
  export default {
    data: function () {
      return {
        tasks: [],
        newTask: ''
      }
    },
    mounted: function () {
      this.fetchTasks();
    },
    methods: {
      fetchTasks: function () {
        axios.get('/api/tasks').then((response) => {
          for (let i = 0; i < response.data.tasks.length; i++) {
            this.tasks.push(response.data.tasks[i])
          }
        }, (error) => {
          console.log(error);
        });
      },
 <中略>
  • npmなどでインストールするVue.jsのライブラリとは異なる外部ライブラリは、 Vue.use で記載しない
    (ここの場合は axios をimportしているが、Vue.js用に作られたライブラリではないので、 Vue.use を使わない)

最後に

  • 理解不足で誤った記載があるかもしれません。もし誤りがありましたら、ご指摘頂ければ幸いです :pray:

2018年7月27日追記

コメント頂いた部分を修正しました。
修正前は、Vue.use( )を使う場合はVue.js公式ライブラリだと記載していましたが、
単にVue.js用に作られたライブラリを使う場合にVue.use( )を使うということで記載修正しています。

43
37
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
37