也许大家都是使用习惯了es5的写法喜欢直接用《Script》标签倒入js文件,但是很是可惜,这写法。在es6,或则说vue环境下不支持


真的写法是怎样?


首先。我们要改造我们要映入的外部js文件,改成下面这个格式。主要是红色方块内部代码,我们需要将我们的模块“抛出”,让人能获取到

Vue如何导入外部JS文件(ES6)

代码:

function realconsole(){
	alert("hello.thanks use me");
 
}
	export {
		realconsole
	}

 


其次,到我们的寄主那里,我们需要导入,仿造其他的文件,写法是这样的:

Vue如何导入外部JS文件(ES6)

代码:

<template>
	<div class="teslist">
		<button @click="methods1">显示console</button>
	</div>
</template>
<script src="../../lib/myconsole.js"></script>
<script>
	import { realconsole } from '../../lib/myconsole.js'
	export default {
		methods:{methods1:function(){
		realconsole();
		}
	}}
</script>
<style>
	.teslist {
	}
</style>

注意红色叉的部分,那是我们es5的写法,绿色才是正确的

接着是效果图

Vue如何导入外部JS文件(ES6)Vue如何导入外部JS文件(ES6)


来源于:

https://blog.csdn.net/hushilin001/article/details/76301368

相关文章:

  • 2022-02-09
  • 2022-12-23
  • 2021-11-21
  • 2021-03-30
  • 2021-12-01
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2021-09-07
  • 2021-06-26
  • 2021-09-29
  • 2022-12-23
  • 2021-11-21
相关资源
相似解决方案