手把手教你写一个Vue组件发布到npm且可外链引入运用
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/37c9101050f8465ea4121c80b38fb3a0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777177&x-signature=E7zOb29VDWA%2BZmEcnZaroiKBe8I%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">手把手教你写一个Vue组件发布到npm且可外链引入<span style="color: black;">运用</span></h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">前言</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">为何</span>要写个组件上传到</span><span style="color: black;"><span style="color: black;">npm</span></span>镜像上呢,<span style="color: black;">咱们</span>肯定遇到过<span style="color: black;">这般</span>一个场景,项目中有<span style="color: black;">非常多</span><span style="color: black;">地区</span>与某个功能<span style="color: black;">类似</span>,你想到的肯定是把该功能封装成<span style="color: black;"><span style="color: black;">Component</span></span>组件,后续方便<span style="color: black;">咱们</span>调用。<span style="color: black;">然则</span>过了一段时间,你的<span style="color: black;"><span style="color: black;">Leader</span></span>让你去<span style="color: black;">研发</span>另一个项目,结果你在哪个项目中又看见了类似的功能,你<span style="color: black;">此时</span>会怎么做? 你<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">运用</span><span style="color: black;"><span style="color: black;">Ctrl + c + v</span></span>大法,拿过来上一个项目封装好的代码,<span style="color: black;">然则</span><span style="color: black;">倘若</span>需求有些变动,你得<span style="color: black;">守护</span>两套项目的代码,<span style="color: black;">乃至</span>以后<span style="color: black;">更加多</span>的项目....,<span style="color: black;">此时</span>你就<span style="color: black;">能够</span>封装一个功能上传到你们<span style="color: black;">机构</span>内网的<span style="color: black;"><span style="color: black;">npm</span></span>上(<span style="color: black;">或</span>自己的账号上),<span style="color: black;">这般</span>每次遇到类似的功能直接<span style="color: black;"><span style="color: black;">npm install</span></span> 安装<span style="color: black;"><span style="color: black;">import</span></span>导入进来<span style="color: black;">运用</span>就<span style="color: black;">能够</span>,需求有变动时完全<span style="color: black;">能够</span>改动一处代码。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置环境</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">笔者<span style="color: black;">这儿</span><span style="color: black;">运用</span>的是</span><span style="color: black;"><span style="color: black;">Webpack</span></span>配置(有点菜,不要介意),<span style="color: black;">亦</span><span style="color: black;">能够</span>安装一个<span style="color: black;"><span style="color: black;">Vue-cli</span></span>简单版的,它那里面有暴露<span style="color: black;"><span style="color: black;">Webpack</span></span>的配置(<span style="color: black;">亦</span>得修改<span style="color: black;">自动</span>配置),<span style="color: black;">咱们</span>来配置一下打包组件环境,<span style="color: black;">通常</span><span style="color: black;">研发</span>组件库都是<span style="color: black;">运用</span>的<span style="color: black;"><span style="color: black;">umd</span></span>格式,这种格式支持<span style="color: black;"><span style="color: black;">Es Module</span></span>、<span style="color: black;"><span style="color: black;">CommonJs</span></span>、<span style="color: black;"><span style="color: black;">AMD</span></span>三种引入方式<span style="color: black;">运用</span>,<span style="color: black;">重点</span><span style="color: black;">便是</span><span style="color: black;"><span style="color: black;">Webpack</span></span>里的<span style="color: black;"><span style="color: black;">library</span></span>和<span style="color: black;"><span style="color: black;">libraryTarget</span></span>,<span style="color: black;">倘若</span>不明白的看<span style="color: black;">这儿</span>详解webpack的out.libraryTarget属性</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">我<span style="color: black;">这儿</span>的Webpack版本为4, 最好跟着本章里的插件版本号进行安装,避免<span style="color: black;">显现</span>版本兼容问题</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">项目结构</h1>|- /node_modules
|- /src
|- Tag.vue
|- main.js
|- index.html
|- webpack.<span style="color: black;">config</span>.js
|- <span style="color: black;">package</span>.json
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">初始化Package.json</h1><span style="color: black;">npm</span> init -y
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">安装Webpack && Loader && Plugin</h1><span style="color: black;">cnpm</span> <span style="color: black;">i webpack webpack-cli -D</span>
<span style="color: black;">cnpm</span> <span style="color: black;">i css-loader style-loader -D</span>
<span style="color: black;">cnpm</span> <span style="color: black;">i file-loader -D</span>
<span style="color: black;">cnpm</span> <span style="color: black;">i vue-loader@15.7.0 vue vue-template-compiler -D</span>
<span style="color: black;">cnpm</span> <span style="color: black;">i html-webpack-plugin@3.2.0 -D</span>
<span style="color: black;">复制代码</span><span style="color: black;">css-loader style-loader 配置</span><span style="color: black;"><span style="color: black;">.css</span></span>文件及样式<span style="color: black;">运用</span><span style="color: black;">file-loader 配置特殊字体和<span style="color: black;">照片</span><span style="color: black;">运用</span></span><span style="color: black;">vue-loader 处理</span><span style="color: black;"><span style="color: black;">.vue</span></span>文件后缀<span style="color: black;">vue <span style="color: black;">运用</span>Vue语法</span><span style="color: black;">vue-template-compiler 处理</span><span style="color: black;"><span style="color: black;">.vue</span></span>文件里的<span style="color: black;"><span style="color: black;">template</span></span>模板语法<h1 style="color: black; text-align: left; margin-bottom: 10px;">webpack.config.js</h1><span style="color: black;">const</span> VueLoaderPlugin = <span style="color: black;">require</span>(<span style="color: black;">vue-loader/lib/plugin</span>)
<span style="color: black;">const</span>HtmlWebpackPlugin =<span style="color: black;">require</span>(<span style="color: black;">"html-webpack-plugin"</span>)
<span style="color: black;">module</span>.exports = {
mode: <span style="color: black;">"development"</span>,
entry: <span style="color: black;">"./src/main.js"</span>,
output: {
filename: <span style="color: black;">"index.js"</span>
},
<span style="color: black;">module</span>: {
rules: [
{
test:<span style="color: black;">/\.css$/</span>,
use: [<span style="color: black;">"style-loader"</span>, <span style="color: black;">"css-loader"</span>]
},
{
test: <span style="color: black;">/\.(ttf|eot|woff|svg|woff2)/</span>,
use: <span style="color: black;">"file-loader"</span>
},
{
test: <span style="color: black;">/\.vue$/</span>,
use: <span style="color: black;">"vue-loader"</span>
}
]
},
plugins: [
<span style="color: black;">new</span> VueLoaderPlugin(),
<span style="color: black;">new</span> HtmlWebpackPlugin({
template: <span style="color: black;">"./index.html"</span>})
]
}
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">index.html</h1><span style="color: black;"><!DOCTYPE <span style="color: black;">html</span>></span>
<span style="color: black;"><<span style="color: black;">html</span> <span style="color: black;">lang</span>=<span style="color: black;">"en"</span>></span>
<span style="color: black;"><<span style="color: black;">head</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">charset</span>=<span style="color: black;">"UTF-8"</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">http-equiv</span>=<span style="color: black;">"X-UA-Compatible"</span> <span style="color: black;">content</span>=<span style="color: black;">"IE=edge"</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">name</span>=<span style="color: black;">"viewport"</span> <span style="color: black;">content</span>=<span style="color: black;">"width=device-width, initial-scale=1.0"</span>></span>
<span style="color: black;"><<span style="color: black;">title</span>></span>Document<span style="color: black;"></<span style="color: black;">title</span>></span>
<span style="color: black;"></<span style="color: black;">head</span>></span>
<span style="color: black;"><<span style="color: black;">body</span>></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">id</span>=<span style="color: black;">"app"</span>></span><span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"></<span style="color: black;">html</span>></span>
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上<span style="color: black;">咱们</span>基本环境就搭建完啦,<span style="color: black;">能够</span>在终端<span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">npx webpack</span></span>运行<span style="color: black;">瞧瞧</span>哦。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">封装组件</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我<span style="color: black;">这儿</span>只做一个示例哈,代码就不写<span style="color: black;">那样</span><span style="color: black;">繁杂</span>,<span style="color: black;">大众</span><span style="color: black;">晓得</span>怎么打包<span style="color: black;">运用</span>就行,<span style="color: black;">详细</span>封装成啥样看你们<span style="color: black;">机构</span>需求啦~。笔者<span style="color: black;">这儿</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">Element Ui</span></span>组件来做一个示例,相信大部分小伙伴<span style="color: black;">机构</span><span style="color: black;">亦</span>在<span style="color: black;">运用</span><span style="color: black;"><span style="color: black;">Element Ui</span></span>。假如<span style="color: black;">咱们</span>项目中有以下类似的功能就<span style="color: black;">能够</span>单独封装起来。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/106dfb75b97240f8945836605deeee0f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723777177&x-signature=75RH9e0Ni3TXWLSYAe9KkmXOShI%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">main.js</h1><span style="color: black;">import</span> Vue <span style="color: black;">from</span> <span style="color: black;">vue</span>
<span style="color: black;">import</span> { Tag } <span style="color: black;">from</span> <span style="color: black;">element-ui</span>;
<span style="color: black;">import</span> <span style="color: black;">element-ui/lib/theme-chalk/tag.css</span>;
<span style="color: black;">import</span> customTag <span style="color: black;">from</span> <span style="color: black;">"./Tag.vue"</span>
Vue.component(Tag.name, Tag)
<span style="color: black;">export</span> <span style="color: black;">default</span> customTag
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">Tag.vue</h1><span style="color: black;"><<span style="color: black;">template</span>></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"Tag"</span>></span>
{{ msg }}
<span style="color: black;"><<span style="color: black;">el-tag</span> <span style="color: black;">type</span>=<span style="color: black;">"success"</span>></span>标签二<span style="color: black;"></<span style="color: black;">el-tag</span>></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"></<span style="color: black;">template</span>></span>
<span style="color: black;"><<span style="color: black;">script</span>></span><span style="color: black;">
<span style="color: black;">export</span> <span style="color: black;">default</span> {
<span style="color: black;">name</span>: <span style="color: black;">Tag</span>,
data() {<span style="color: black;">return</span> {
<span style="color: black;">msg</span>: <span style="color: black;">"hello 蛙人"</span>,
}
},
created() {
},
<span style="color: black;">components</span>: {},
<span style="color: black;">watch</span>: {},
<span style="color: black;">methods</span>: {
}
}
</span><span style="color: black;"></<span style="color: black;">script</span>></span>
<span style="color: black;"><<span style="color: black;">style</span> <span style="color: black;">scoped</span>></span>
<span style="color: black;"></<span style="color: black;">style</span>></span>
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">Webpack.config.js</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">将</span><span style="color: black;"><span style="color: black;">webpack.config.js</span></span>里的<span style="color: black;"><span style="color: black;">output</span></span>修改为如下</p><span style="color: black;">output</span>: {
<span style="color: black;">filename</span>: <span style="color: black;">"index.js"</span>,
library: <span style="color: black;">"Modal"</span>,
libraryTarget: <span style="color: black;">"umd"</span>}
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">配置完之后就<span style="color: black;">能够</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">npx webpack</span></span>打包,<span style="color: black;">能够</span>看到有一个<span style="color: black;"><span style="color: black;">dist</span></span>目录,该目录下存在一个<span style="color: black;"><span style="color: black;">index.js</span></span>, 这个文件<span style="color: black;">便是</span><span style="color: black;">咱们</span>封装的<span style="color: black;"><span style="color: black;">Tag.vue</span></span>文件, 你<span style="color: black;">能够</span>将它引入到你的项目中,进行调用,该文件支持<span style="color: black;"><span style="color: black;">Es Module</span></span>、<span style="color: black;"><span style="color: black;">CommonJs</span></span>、<span style="color: black;"><span style="color: black;">AMD</span></span>三种方式引入。</p><span style="color: black;">import</span> Vue <span style="color: black;">from</span> <span style="color: black;">vue</span>
<span style="color: black;">import</span> { Tag } <span style="color: black;">from</span> <span style="color: black;">element-ui</span>;
<span style="color: black;">import</span> <span style="color: black;">element-ui/lib/theme-chalk/tag.css</span>;
Vue.component(Tag.name, Tag)
<span style="color: black;">import</span> CustomTag <span style="color: black;">from</span> <span style="color: black;">"./index"</span> <span style="color: black;">// 打包完的,直接引入进来</span>
<span style="color: black;">new</span> Vue({
<span style="color: black;">el</span>: <span style="color: black;">"#app"</span>,
<span style="color: black;">render</span>: <span style="color: black;"><span style="color: black;">h</span> =></span> h(CustomTag)
})
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">Npm发布</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span><span style="color: black;">无</span></span><span style="color: black;"><span style="color: black;">npm</span></span>账号呢,先去官网注册一个<span style="color: black;"><span style="color: black;">npm</span></span>账号<span style="color: black;">这儿</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">新建一个发布包项目文件夹</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在终端执行</span><span style="color: black;"><span style="color: black;">npm init -y</span></span> ,进行初始<span style="color: black;"><span style="color: black;">package.json</span></span>文件,<span style="color: black;">重点</span>信息<span style="color: black;">便是</span>name和main字段,前者是这个包的名<span style="color: black;">叫作</span>(<span style="color: black;">亦</span><span style="color: black;">便是</span>npm instal xxx),后者则是<span style="color: black;">咱们</span>打包好的文件<span style="color: black;"><span style="color: black;">Tag</span></span>文件,默认<span style="color: black;"><span style="color: black;">main</span></span>就去找这个入口文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">重视</span>:包名<span style="color: black;">叫作</span><span style="color: black;">不可</span><span style="color: black;">包括</span>大写,包名<span style="color: black;">叫作</span><span style="color: black;">不可</span><span style="color: black;">包括</span>大写,包名<span style="color: black;">叫作</span><span style="color: black;">不可</span><span style="color: black;">包括</span>大写,重要的事情说三遍</span></span></p>{
<span style="color: black;">"name"</span>: <span style="color: black;">"custom-tag-waren"</span>,
<span style="color: black;">"version"</span>: <span style="color: black;">"1.0.0"</span>,<span style="color: black;">"description"</span>: <span style="color: black;">"这是xxxx"</span>,
<span style="color: black;">"main"</span>: <span style="color: black;">"index.js"</span>,
<span style="color: black;">"scripts"</span>: {
<span style="color: black;">"test"</span>: <span style="color: black;">"echo \"Error: no test specified\" && exit 1"</span>
},
<span style="color: black;">"keywords"</span>: [],
<span style="color: black;">"author"</span>: <span style="color: black;">"WaRen"</span>,
<span style="color: black;">"license"</span>: <span style="color: black;">"ISC"</span>
}
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>淘宝镜像之前被更改,先改回来执行以下命令</span></p>npm config <span style="color: black;">set</span> registry http:<span style="color: black;">//registry.npmjs.org</span>
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">注册完之后,执行</span><span style="color: black;"><span style="color: black;">npm login</span></span>, 依次填写你的<span style="color: black;"><span style="color: black;">用户名</span></span>、<span style="color: black;"><span style="color: black;"><span style="color: black;">秘码</span></span></span>、<span style="color: black;"><span style="color: black;">邮箱</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">执行</span><span style="color: black;"><span style="color: black;">npm publish</span></span>发布,<span style="color: black;">而后</span>等待进度条完成<span style="color: black;">就可</span>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">整理<span style="color: black;">有些</span><span style="color: black;">平常</span>的发布错误</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是<span style="color: black;">由于</span>镜像设置成淘宝镜像了,设置回来<span style="color: black;">就可</span></span></p>no_perms Private mode enable, only admin can publish <span style="color: black;">this</span> <span style="color: black;">module</span>
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">通常</span>是<span style="color: black;">无</span>登录,重新登录一下 </span><span style="color: black;"><span style="color: black;">npm login</span></span> <span style="color: black;">就可</span></p><span style="color: black;">npm</span> <span style="color: black;">publish</span> <span style="color: black;">failed</span> <span style="color: black;">put</span> <span style="color: black;">500</span> <span style="color: black;">unexpected</span> <span style="color: black;">status</span> <span style="color: black;">code</span> <span style="color: black;">401</span>
<span style="color: black;">复制代码</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">包名被占用,改个包名<span style="color: black;">就可</span>,最好在官网查一下<span style="color: black;">是不是</span>有包名被占用,之后再重命名</span></p>npm ERR! you <span style="color: black;">do</span> <span style="color: black;">not</span> have permission <span style="color: black;">to</span> publish “your <span style="color: black;">module</span> <span style="color: black;">name</span>”. <span style="color: black;">Are</span> you logged <span style="color: black;">in</span> <span style="color: black;">as</span> the correct <span style="color: black;">user</span>?
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">邮箱未验证,去官网验证一下邮箱</span></p>you must verify your email before publishing a <span style="color: black;">new</span> <span style="color: black;">package</span>
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">npm安装<span style="color: black;">运用</span></h1><span style="color: black;">cnpm</span> i custom-tag-waren -D
复制代码<h1 style="color: black; text-align: left; margin-bottom: 10px;">main.js</h1><span style="color: black;">import</span> Vue <span style="color: black;">from</span> <span style="color: black;">vue</span>
<span style="color: black;">import</span> { Tag } <span style="color: black;">from</span> <span style="color: black;">element-ui</span>;
<span style="color: black;">import</span> <span style="color: black;">element-ui/lib/theme-chalk/tag.css</span>;
<span style="color: black;">import</span> customTagWaren <span style="color: black;">from</span> <span style="color: black;">"custom-tag-waren"</span> <span style="color: black;">// 下载完引入进来</span>
Vue.component(Tag.name, Tag)
<span style="color: black;">new</span>Vue({<span style="color: black;">el</span>: <span style="color: black;">"#app"</span>,
<span style="color: black;">render</span>: <span style="color: black;"><span style="color: black;">h</span> =></span> h(customTagWaren)
})
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">到此为止就完<span style="color: black;">成为了</span>一个组件的打包上传下载,<span style="color: black;">这般</span><span style="color: black;">咱们</span>在<span style="color: black;">每一个</span>项目<span style="color: black;">必须</span>的时候直接</span><span style="color: black;"><span style="color: black;">npm install</span></span>安装就行,当需求改动的时候只改一个文件<span style="color: black;">而后</span>再次发布就行。是不是很方便啦。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">外链引入</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">亦</span>不上传</span><span style="color: black;"><span style="color: black;">npm</span></span>上,直接<span style="color: black;">运用</span>外链的形式<span style="color: black;">运用</span>,下面<span style="color: black;">咱们</span>来<span style="color: black;">瞧瞧</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">import引入</h1><span style="color: black;"><<span style="color: black;">template</span>></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">class</span>=<span style="color: black;">"Tag"</span>></span>
<span style="color: black;"><<span style="color: black;">TagEl</span>/></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"></<span style="color: black;">template</span>></span>
<span style="color: black;"><<span style="color: black;">script</span>></span><span style="color: black;">
<span style="color: black;">import</span> TagEl <span style="color: black;">from</span> <span style="color: black;">"./index"</span>
<span style="color: black;">export</span> <span style="color: black;">default</span> {
<span style="color: black;">name</span>: <span style="color: black;">Tag</span>,
data() {<span style="color: black;">return</span> {
}
},
<span style="color: black;">components</span>: {
TagEl
},
}
</span><span style="color: black;"></<span style="color: black;">script</span>></span>
<span style="color: black;"><<span style="color: black;">style</span> <span style="color: black;">scoped</span>></span>
<span style="color: black;"></<span style="color: black;">style</span>></span>
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面</span><span style="color: black;"><span style="color: black;">example</span></span>中,<span style="color: black;">咱们</span>看到直接引入了<span style="color: black;"><span style="color: black;">index.js</span></span>文件并进行注册组件,直接就<span style="color: black;">能够</span><span style="color: black;">运用</span>啦。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">script引入</h1><span style="color: black;"><!DOCTYPE <span style="color: black;">html</span>></span>
<span style="color: black;"><<span style="color: black;">html</span> <span style="color: black;">lang</span>=<span style="color: black;">"en"</span>></span>
<span style="color: black;"><<span style="color: black;">head</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">charset</span>=<span style="color: black;">"UTF-8"</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">http-equiv</span>=<span style="color: black;">"X-UA-Compatible"</span> <span style="color: black;">content</span>=<span style="color: black;">"IE=edge"</span>></span>
<span style="color: black;"><<span style="color: black;">meta</span> <span style="color: black;">name</span>=<span style="color: black;">"viewport"</span> <span style="color: black;">content</span>=<span style="color: black;">"width=device-width, initial-scale=1.0"</span>></span>
<span style="color: black;"><<span style="color: black;">title</span>></span>Document<span style="color: black;"></<span style="color: black;">title</span>></span>
<span style="color: black;"></<span style="color: black;">head</span>></span>
<span style="color: black;"><<span style="color: black;">body</span>></span>
<span style="color: black;"><<span style="color: black;">div</span> <span style="color: black;">id</span>=<span style="color: black;">"app"</span>></span>
<span style="color: black;"><<span style="color: black;">Tag</span>/></span>
<span style="color: black;"></<span style="color: black;">div</span>></span>
<span style="color: black;"><<span style="color: black;">script</span> <span style="color: black;">src</span>=<span style="color: black;">"https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"</span>></span><span style="color: black;"></<span style="color: black;">script</span>></span>
<span style="color: black;"><<span style="color: black;">script</span> <span style="color: black;">type</span>=<span style="color: black;">"text/javascript"</span> <span style="color: black;">src</span>=<span style="color: black;">"./dist/index.js"</span>></span><span style="color: black;"></<span style="color: black;">script</span>></span>
<span style="color: black;"></<span style="color: black;">body</span>></span>
<span style="color: black;"><<span style="color: black;">script</span>></span><span style="color: black;">
<span style="color: black;">new</span> Vue({
<span style="color: black;">el</span>: <span style="color: black;">"#app"</span>,
<span style="color: black;">components</span>: {
<span style="color: black;">Tag</span>: Tag.default
}
})</span><span style="color: black;"></<span style="color: black;">script</span>></span>
<span style="color: black;"></<span style="color: black;">html</span>></span>
复制代码<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面</span><span style="color: black;"><span style="color: black;">example</span></span>中,直接<span style="color: black;">运用</span><span style="color: black;"><span style="color: black;">script</span></span>标签引入进来,<span style="color: black;">亦</span>是注册完<span style="color: black;">运用</span>就<span style="color: black;">能够</span>。<span style="color: black;">那样</span><span style="color: black;">咱们</span>怎么<span style="color: black;">晓得</span>他名字是Tag,这个你在封装组件的<span style="color: black;">运用</span>,<span style="color: black;">必要</span>指定Name名<span style="color: black;">叫作</span>。</p><span style="color: black;">export</span> <span style="color: black;">default</span> {
<span style="color: black;">name</span>: <span style="color: black;">"Tag"</span>}
复制代码
太棒了、厉害、为你打call、点赞、非常精彩等。 外贸网站建设方法 http://www.fok120.com/ “BS”(鄙视的缩写) 感谢楼主的分享!我学到了很多。 你的话语如春风拂面,温暖了我的心房,真的很感谢。
页:
[1]