Vue 中 import 引入的方法在template模板中使用

在 methods 声明一下就可以用了

<template>
  <div>{{changeAttrVal}}</div>
</template>
<script>
import { changeImage, changeAttrVal } from "@/utils";

methods: {
    changeAttrVal,
    changeImage,
    test(){
    	// 其他方法
    }
}

组件的话,比如在一个vue写成的页面中,想要使用一个vue完成的表格组件(这个表格组件也是一个XXX.vue文件),具体的怎么在一个文件中引入需要使用的那部分内容,比如前面的表格、比如某个饼图图表什么的。
下面有两种方法:

方法一:代码如下

<template>
<div>
<!-- 3.在template中就可以直接使用了 -->
<testComponent></testComponent>
</div>
</template>

<script>
//1.先使用import导入你要在该组件中使用的子组件
import testComponent from './testComponent.vue'
export default {
//2.然后,在components中写入子组件
components: {testComponent},
methods: {},
}
</script>

方法二:代码如下

<template>
<div>
   <!--2.在template 中使用-->
 
</div>

</template>
export default {
//1.直接在components属性中写入子组件
components: {
testComponent:require('./testComponent.vue').default
},
methods: {},

}
</script>

两者之间的区别:初学者暂时没发现啥显著的区别,第一种应该是使用的es6的引入机制,第二种使用的是es5
的机制,所以,推荐还是使用第一种好点。

//正常加载

import index from '../pages/index.vue'

import view from '../pages/view.vue'

//懒加载

const index = resolve => require(['../pages/index.vue'], resolve)

const view = resolve => require(['../pages/view.vue'], resolve)

//懒加载 -按组

const index = r => require.ensure([], () => r(require('../pages/index.vue')), 'group-index')

const view = r => require.ensure([], () => r(require('../pages/view.vue')), 'group-view')

// 懒加载 - 按组 import,基于ES6 import的特性

const index = () => import('../pages/index.vue')

const view = () => import('../pages/view.vue')

参考:
https://blog.csdn.net/qq_17627195/article/details/124087042
https://www.cnblogs.com/feicheninfo/articles/10411955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值