使用CLI 3 建立發佈Web Components

本文翻譯自:codementor
javascript

翻譯不當之處,歡迎指正交流html

Web Components是web平臺的將來嗎?關於這一問題支持和反對的觀點有不少。事實上瀏覽器對Web Components的支持正在逐漸造成,並有愈來愈多的工具、資源和IT從業人員正在致力於建立發佈本身的Web Components。vue

Vue.js是一個建立Web Components的很好的工具,在更新的Vue CLI 3和@vue/web-component-wrapper中甚至更加簡單。
這篇文章中,咱們會討論爲何你須要建立一個Web Components並向你展現如何在僅有Vue基礎知識的狀況下建立你的第一個Web Components。
Note: this article was originally posted here on the Vue.js Developers blog on 2018/05/21 [本文章原創發佈地址]
java

 

  • 什麼是Web Components

相信你已經對HTML元素(div,spans,tables等)很熟悉了,Web Components是一種定製的,能夠在web app和web頁面中使用並複用的HTML元素。
例如,你能夠建立一個定製的element叫作 video-player ,而且
能夠提供一個可重用的video接口, 它的 UI 功能超出了標準 HTML 5 視頻元素的可用範圍。這個元素可以爲video文件和事件(如:播放play,暫停pause等)提供一個「src」屬性,容許用戶以編程方式來控制它。react

<div>
<video-player src="..." onpause="..."></video-player>
</div>

這聽起來或許很像常規的Vue components作的,不一樣之處就在於web components是瀏覽器原生的(或者說至少會隨着規範來逐步實現)並可以像HTML元素同樣來用。不管如何,無論你用什麼工具去建立你的web components你均可以在react,angular等框架中(甚至不須要使用框架)來使用它。webpack

function ReactComponent() { return( <h1>A Vue.js web component used in React! </h1>
    <video-player></video-player> ); }

 

  • 如何建立一個web component?

在內部,web components由瀏覽器早已熟悉的標準HTML元素構成,如divs,spans等。因此video-player在內部看起來更像這樣:git

<div>
  <video src="..."></video>
  <div class="buttons">
    <button class="play-button"></button>
    <button class="pause-button"></button> ... </div> ... </div>

Web components也能夠包含CSS和Javascript。使用新的瀏覽器標準像Shadow DOM這種,經過你本身定製的組件徹底封裝,所以用戶不須要擔憂CSS會覆蓋web component中的規則。
固然,您將用API聲明自帶的web 組件。但咱們目前不須要知道,由於咱們將使用的的是一個抽象層
更多深刻介紹 Web Components - Introduction
github

 

  • 使用@vue/web-component-wrapper建立web components

經過Vue CLI3和新的@vue/web-component-wrapper庫建立web components十分方便。
@vue/web-component-wrapper庫提供了一個經過web componentAPI接入Vue組件的容器。這個容器可以自動代理properties,attributes,events和slot。這意味着你能夠僅用你現有的Vue components知識寫一個web components!
關於建立webcomponents另外一個vue庫vue-custom-element
建立一個webcomponent,先確保已安裝Vue CLI3並經過任何你熟悉的環境建立一個新的項目。web

$ vue create vue-web-component-project

如今建立一個新的能夠做爲web component來使用的Vue component.這個組件在發佈前能夠經過webpack來編輯,因此你可使用任何JavaScript的特性。但這裏咱們只是作一些簡單的雛形來進行可行性的概念驗證:
src/components/VueWebComponent.vueajax

<template>
  <div>
    <h1>My Vue Web Component</h1>
    <div>{{ msg }}</div>
  </div>
</template>
<script> export default { props: ['msg'] } </script>

經過@vue/web-component-wrapper來準備一個包裹的組件以確保你的入口文件,正如: src/main.js

import Vue from 'vue';
import wrap from '@vue/web-component-wrapper';
import VueWebComponent from './components/VueWebComponent';

const CustomElement = wrap(Vue, VueWebComponent);

window.customElements.define('my-custom-element', CustomElement);

用來註冊一個組建的APIcustomElements.define()注意custom element和web component在這方面是同樣的

 

  • 用Vue CLI3構建一個web component

Vue CLI3包括了許多不錯的新特性(查看這篇文章的摘要)。CLI Service使用的是Webpack來處理多項任務包括構建你本身的項目代碼。這一點能夠經過簡單的vue-cli-service build指令來完成。經過添加

--target wc來轉換,你能夠建立一個依賴,完美地建立一個web component:

$ vue-cli-service build --target wc --name my-custom-element ./src/main.js

在幕後,使用webpack來處理單獨的javascript文件和全部必須的內置web components。當被包含在一個頁面內時,<my-custom-element> 這個腳本註冊了一個用@vue/web-component-wrapper包裹着目標的Vue組件

 

  • 在網頁中使用你的Vue web component

隨着你的組建的構建, 任何人均可以在一個非Vue項目中使用它使用並不須要任何Vue.js的代碼(即使你爲了不重複刻意不添加綁定而須要導入Vue庫 做爲在這種狀況下,where你使用多個基於Vue的web components)一旦你加載定義在頁面上的腳本時,定製的元素就起到了原生HTML元素的做用。
請注意, 包含polyfill(填充代碼)是很是必要的, 由於大多數瀏覽器自己並不支持全部web component規範。在這裏,我使用webcomponents.js (v1 spec polyfills)
index.html

<!DOCTYPE html>
<html>
  <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>My Non-Vue App</title>
  </head>
  <body>
    <!--Load Vue-->
    <script src="https://unpkg.com/vue"></script>
    <!--Load the web component polyfill-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.2.0/webcomponents-loader.js"></script>
    <!--Load your custom element-->
    <script src="./my-custom-element.js"></script>
    <!--Use your custom element-->
    <my-custom-element msg="Hello..."></my-custom-element>
  </body>
</html>

這段代碼是有效的,若是你想把這段代碼做爲模板來引用的話,我把它們放到了這裏

 

  • 發佈

最後,如果你想和全部人分享你的web component,沒有比webcomponents.org更好的地方了。這個網站有一個供免費下載web component的可瀏覽收藏夾,所展現的是由Vue,Polymer,Abgular等各類框架構建的組件。

擴展閱讀
• Docs for @vue/web-component-wrapper
• Docs for Vue CLI 3 Build Targets

• Web Components - Introduction

Get the latest Vue.js articles, tutorials and cool projects in your inbox with the Vue.js Developers Newsletter

相關文章
相關標籤/搜索