Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
、HTML 塊元素
二、HTML 內(nèi)聯(lián)元素
三、HTML <div> 元素
四、div的使用
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無標(biāo)題文檔</title> </head> <style> .all{ width:500px; height:500px; margin:0 auto; background-color:#000; } .one{ height:100px; background-color:#89E1BF; } .two{ height:100px; background-color:#DEE099; } .three{ height:100px; background-color:#D7A1CE; } </style> <body> <!--父div,all是黑色--> <div class="all"> <!--子div,one是綠色--> <div class="one"> </div> <!--子divtwo,是黃色--> <div class="two"> </div> <!--子div,three是紫色--> <div class="three"> </div> </div> </body> </html>
演示效果如圖所示:
. 嵌套操作----------
子操作: >
div>ul>li
<div>
<ul>
<li></li>
</ul>
</div>
并列:+
div+ul>li
<div></div>
<ul>
<li></li>
</ul>
上級(jí):^
ul>li^div
<ul>
<li></li>
</ul>
<div></div>
ul>li>a^^div 上級(jí)多層
<ul>
<li><a href=""></a></li>
</ul>
<div></div>
重復(fù):*
ul>li*3
<ul>
<li></li>
<li></li>
<li></li>
</ul>
分組:()
div>(p>span)*2
<div>
<p><span></span></p>
<p><span></span></p>
</div>
2. 屬性操作----------
id和類
div#header+div.main+div#footer
<div id="header"></div>
<div class="main"></div>
<div id="footer"></div>
屬性值
a[title=test target=_self]
<a title="test" target="_self" href=""></a>
數(shù)列值:$
p.item$*3
<p class="item1"></p>
<p class="item2"></p>
<p class="item3"></p>
p.item$$*3
<p class="item01"></p>
<p class="item02"></p>
<p class="item03"></p>
數(shù)列操作符:@
p.item$@-*3 @- = -1
<p class="item3"></p>
<p class="item2"></p>
<p class="item1"></p>
p.item$@3*3 @3 = 從3開始3次
<p class="item3"></p>
<p class="item4"></p>
<p class="item5"></p>
p.item$@-3*3 @-3 = 3次后到3結(jié)束
<p class="item5"></p>
<p class="item4"></p>
<p class="item3"></p>
3. 字符操作----------
字符操作:{}
a{click}
<a href="">click</a>
a>{click}+span{me}
<a href="">click<span>me</span></a>
4. 缺省元素----------
.header+.footer --------------- div.header+div.footer
ul>.item*3 -------------- ul>li.item*3
table>.row*4>.cell*3 -------------- table>tr.row*4>td.cell*3
我們?yōu)槭裁匆獙憘€(gè)組件上傳到npm鏡像上呢,我們肯定遇到過這樣一個(gè)場(chǎng)景,項(xiàng)目中有很多地方與某個(gè)功能相似,你想到的肯定是把該功能封裝成Component組件,后續(xù)方便我們調(diào)用。但是過了一段時(shí)間,你的Leader讓你去開發(fā)另一個(gè)項(xiàng)目,結(jié)果你在哪個(gè)項(xiàng)目中又看見了類似的功能,你這時(shí)會(huì)怎么做? 你也可以使用Ctrl + c + v大法,拿過來上一個(gè)項(xiàng)目封裝好的代碼,但是如果需求有些變動(dòng),你得維護(hù)兩套項(xiàng)目的代碼,甚至以后更多的項(xiàng)目....,這時(shí)你就可以封裝一個(gè)功能上傳到你們公司內(nèi)網(wǎng)的npm上(或者自己的賬號(hào)上),這樣每次遇到類似的功能直接npm install 安裝import導(dǎo)入進(jìn)來使用就可以,需求有變動(dòng)時(shí)完全可以改動(dòng)一處代碼。
筆者這里使用的是Webpack配置(有點(diǎn)菜,不要介意),也可以安裝一個(gè)Vue-cli簡(jiǎn)單版的,它那里面有暴露Webpack的配置(也得修改自行配置),我們來配置一下打包組件環(huán)境,一般開發(fā)組件庫都是使用的umd格式,這種格式支持Es Module、CommonJs、AMD三種引入方式使用,主要就是Webpack里的library和libraryTarget,如果不明白的看這里詳解webpack的out.libraryTarget屬性
我這里的Webpack版本為4, 最好跟著本章里的插件版本號(hào)進(jìn)行安裝,避免出現(xiàn)版本兼容問題
|- /node_modules
|- /src
|- Tag.vue
|- main.js
|- index.html
|- webpack.config.js
|- package.json
復(fù)制代碼
npm init -y
復(fù)制代碼
cnpm i webpack webpack-cli -D
cnpm i css-loader style-loader -D
cnpm i file-loader -D
cnpm i vue-loader@15.7.0 vue vue-template-compiler -D
cnpm i html-webpack-plugin@3.2.0 -D
復(fù)制代碼
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require("html-webpack-plugin")
module.exports = {
mode: "development",
entry: "./src/main.js",
output: {
filename: "index.js"
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /\.(ttf|eot|woff|svg|woff2)/,
use: "file-loader"
},
{
test: /\.vue$/,
use: "vue-loader"
}
]
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: "./index.html"
})
]
}
復(fù)制代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
</html>
復(fù)制代碼
以上我們基本環(huán)境就搭建完啦,可以在終端使用npx webpack運(yùn)行看看哦。
我這里只做一個(gè)示例哈,代碼就不寫那么復(fù)雜,大家知道怎么打包使用就行,具體封裝成啥樣看你們公司需求啦~。筆者這里使用Element Ui組件來做一個(gè)示例,相信大部分小伙伴公司也在使用Element Ui。假如我們項(xiàng)目中有以下類似的功能就可以單獨(dú)封裝起來。
import Vue from 'vue'
import { Tag } from 'element-ui';
import 'element-ui/lib/theme-chalk/tag.css';
import customTag from "./Tag.vue"
Vue.component(Tag.name, Tag)
export default customTag
復(fù)制代碼
<template>
<div class="Tag">
{{ msg }}
<el-tag type="success">標(biāo)簽二</el-tag>
</div>
</template>
<script>
export default {
name: 'Tag',
data() {
return {
msg: "hello 蛙人",
}
},
created() {
},
components: {},
watch: {},
methods: {
}
}
</script>
<style scoped>
</style>
復(fù)制代碼
將webpack.config.js里的output修改為如下
output: {
filename: "index.js",
library: "Modal",
libraryTarget: "umd"
}
復(fù)制代碼
配置完之后就可以使用npx webpack打包,可以看到有一個(gè)dist目錄,該目錄下存在一個(gè)index.js, 這個(gè)文件就是我們封裝的Tag.vue文件, 你可以將它引入到你的項(xiàng)目中,進(jìn)行調(diào)用,該文件支持Es Module、CommonJs、AMD三種方式引入。
import Vue from 'vue'
import { Tag } from 'element-ui';
import 'element-ui/lib/theme-chalk/tag.css';
Vue.component(Tag.name, Tag)
import CustomTag from "./index" // 打包完的,直接引入進(jìn)來
new Vue({
el: "#app",
render: h => h(CustomTag)
})
復(fù)制代碼
如果沒有npm賬號(hào)呢,先去官網(wǎng)注冊(cè)一個(gè)npm賬號(hào)這里
在終端執(zhí)行npm init -y ,進(jìn)行初始package.json文件,主要信息就是name和main字段,前者是這個(gè)包的名稱(也就是npm instal xxx),后者則是我們打包好的文件Tag文件,默認(rèn)main就去找這個(gè)入口文件。
注意:包名稱不能包含大寫,包名稱不能包含大寫,包名稱不能包含大寫,重要的事情說三遍
{
"name": "custom-tag-waren",
"version": "1.0.0",
"description": "這是xxxx",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "WaRen",
"license": "ISC"
}
復(fù)制代碼
如果淘寶鏡像之前被更改,先改回來執(zhí)行以下命令
npm config set registry http://registry.npmjs.org
復(fù)制代碼
注冊(cè)完之后,執(zhí)行npm login, 依次填寫你的用戶名、密碼、郵箱
執(zhí)行npm publish發(fā)布,然后等待進(jìn)度條完成即可。
這是因?yàn)殓R像設(shè)置成淘寶鏡像了,設(shè)置回來即可
no_perms Private mode enable, only admin can publish this module
復(fù)制代碼
一般是沒有登錄,重新登錄一下 npm login 即可
npm publish failed put 500 unexpected status code 401
復(fù)制代碼
包名被占用,改個(gè)包名即可,最好在官網(wǎng)查一下是否有包名被占用,之后再重命名
npm ERR! you do not have permission to publish “your module name”. Are you logged in as the correct user?
復(fù)制代碼
郵箱未驗(yàn)證,去官網(wǎng)驗(yàn)證一下郵箱
you must verify your email before publishing a new package
復(fù)制代碼
cnpm i custom-tag-waren -D
復(fù)制代碼
import Vue from 'vue'
import { Tag } from 'element-ui';
import 'element-ui/lib/theme-chalk/tag.css';
import customTagWaren from "custom-tag-waren" // 下載完引入進(jìn)來
Vue.component(Tag.name, Tag)
new Vue({
el: "#app",
render: h => h(customTagWaren)
})
復(fù)制代碼
到此為止就完成了一個(gè)組件的打包上傳下載,這樣我們?cè)诿總€(gè)項(xiàng)目需要的時(shí)候直接npm install安裝就行,當(dāng)需求改動(dòng)的時(shí)候只改一個(gè)文件然后再次發(fā)布就行。是不是很方便啦。
我們也不上傳npm上,直接使用外鏈的形式使用,下面我們來看看
<template>
<div class="Tag">
<TagEl/>
</div>
</template>
<script>
import TagEl from "./index"
export default {
name: 'Tag',
data() {
return {
}
},
components: {
TagEl
},
}
</script>
<style scoped>
</style>
復(fù)制代碼
上面example中,我們看到直接引入了index.js文件并進(jìn)行注冊(cè)組件,直接就可以使用啦。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<Tag/>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
<script type="text/javascript" src="./dist/index.js"></script>
</body>
<script>
new Vue({
el: "#app",
components: {
Tag: Tag.default
}
})
</script>
</html>
復(fù)制代碼
上面example中,直接使用script標(biāo)簽引入進(jìn)來,也是注冊(cè)完使用就可以。那么我們?cè)趺粗浪质荰ag,這個(gè)你在封裝組件的使用,必須指定Name名稱。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。