有很多特效素材都是使用js+html+第三方库制作,本身使用常规的html开发方式,类似:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>测试</title>
<script type="text/javascript" src="Test.js"></script>
</head>
<body>
<div id='myId'>测试</div>
<script>
var test = new Test('myId');
</script>
</body>
</html>
设有一个canvas特效素材,现在希望将该素材整体导入到Vue中,像使用图片一样来使用该素材。
这样的一堆文件,要导入到Vue中,需要使用iframe
。流程为:
- 在 项目根目录/public/ 下创建一个test文件夹,将所有的素材文件复制到这里。
- 创建一个vue文件,使用
iframe
将素材的html导入即可。
vue文件内容为:
<template>
<div class="test-container">
<iframe :src="url" width="100%" height="100%" frameborder="0" scrolling="auto" />
</div>
</template>
<script>
export default {
data() {
return {
url: './test/index.html'
}
},
methods: {
}
}
</script>
<style lang="scss">
.test-container {
width: 100%;
height: 100%;
position: absolute;
}
</style>
注意引入素材的html时,必须使用./
开头,且该路径中不需要包含public。
若使用/
开头,调试正常,放到生产环境下可能会提示The origin server did not find a current representation for the target resource or is not willing to disclose that one exists: