javascript - Vue - global import - Stack Overflow

I useimport {mapFields} from "vuex-map-fields"in every ponent. How can I make this available

I use

import {mapFields} from "vuex-map-fields"

in every ponent.

How can I make this available in every ponent without having to import it? Is there any risk to this?

I use

import {mapFields} from "vuex-map-fields"

in every ponent.

How can I make this available in every ponent without having to import it? Is there any risk to this?

Share Improve this question asked Dec 4, 2018 at 9:27 TommyDTommyD 1,0435 gold badges19 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

if you want it globally what you have to do it's inject in the Vue instance same way that we do when we use vuex

main.js

import Vue from 'vue'
import store from './store'

new Vue( {
  el: '#app',
  store,
  render: h => h( App )
} );

then in any ponent you can console.log(this); you should be able to see your object

you can use Vue.prototype as follows..

[src/main.js]

import { mapFields } from 'vuex-map-fields';
...
Vue.prototype.$mapFields = mapFields; 

then in any ponent you can use this.$mapFields(...) function!

[ in any ponent ]

<template>
......
</template>

<script>
export default {
  puted: {
    ...this.$mapFields({
      userFirstName: 'user.firstName',
      userLastName: 'user.lastName',
    }),
  },
};
</script>

There is no risks in this code...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743728797a4497078.html

相关推荐

  • javascript - Vue - global import - Stack Overflow

    I useimport {mapFields} from "vuex-map-fields"in every ponent. How can I make this available

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信