vueで作ったモジュールをvue-cli-service build --target lib
でビルドして、common js形式とumd形式のjsを作りました。
このモジュールをchromeブラウザ上で動かす方法を教えてください。
それぞれのファイルの先頭、モジュールをexportしている部分はこの様な感じです。
このサンプルに出てくるGoogleDocListというのは、今作っているvueコンポーネントの名前です。
js
1//commom.js 2module.exports = 3/******/ (function(modules) { // webpackBootstrap 4/******/ function hotDisposeChunk(chunkId) { 5/******/ delete installedChunks[chunkId]; 6/******/ } 7略
js
1// umd.js 2(function webpackUniversalModuleDefinition(root, factory) { 3 if(typeof exports === 'object' && typeof module === 'object') 4 module.exports = factory(require("vue")); 5 else if(typeof define === 'function' && define.amd) 6 define([], factory); 7 else if(typeof exports === 'object') 8 exports["GoogleDocList"] = factory(require("vue")); 9 else 10 root["GoogleDocList"] = factory(root["Vue"]); 11})(this, function(__WEBPACK_EXTERNAL_MODULE_vue__) { 12return /******/ (function(modules) { // webpackBootstrap 13略
このモジュールをchromeの中で以下のような書き方で使いたいですがエラーが出てしまいます。
html
1//index.html 2<script src="./module/index.js" type="module"></script>
js
1//module/index.js 2// importの方法はいろいろ試したがどれも駄目。 3import GoogleDocList from './common or umd.js'; 4import * as GoogleDocList from './common or umd.js'; 5import {GoogleDocList } from './common or umd.js'; 6 7const v = new GoogleDocList({ 8 propsData:{} 9}) 10v.$mount("#hogehoge");
chrome上でモジュールを使うには、以下のような書き方をする必要があると思うのですが、commom.js umd.js のどちらもその様な書き方にはなっていません。
js
1export class GoogleDocList { 2 // hogehoge 3}
これは vue-cli-service
で何かオプションの指定が不足しているのでしょうか、
それとも module/index.js
でのimportの書き方が間違っているのでしょうか。教えてください。
動くけど避けたい書き方
今までは以下の書き方で書いていたのですが、この書き方ですとGoogleDocListがグローバルスコープに定義されてしまうので避けたいです。
html
1//index.html 2<script src="./module/umd.js" defer></script> 3<script src="./module/index.js" defer></script>
js
1//module/index.js 2// GoogleDocList 型はこのファイルの中でだけ参照できればいい 3const v = new GoogleDocList({ 4 propsData:{} 5}) 6v.$mount("#hogehoge");

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/27 20:50