mkdir vue-app
cd vue-app
npm init -y
npm install --save-dev vue @vue/cli http-server
- Create
src
folder - Create
./src/Card.vue
please see code - Create
./src/App.vue
please see code - Create
./src/main.js
please code code - Add or Update
scripts
inpackage.json
serve
run your vue app into dev modebuild.card.wc
bundle and wrap yourCard
Component intoweb components
start.wc
start or run your web component bundle into dev mode
"scripts": { "serve": "vue serve ./src/main.js", "build.card.wc": "vue build --target wc --name ar-card ./src/Card.vue", "start.wc": "http-server ./dist", },
- Run your vue app
npm run serve
- Bundle your vue
Card
Component into web componentnpm run build.card.wc
- Run your bundle
web component
- Update
./dist/demo.html
<style> #cards { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: auto; grid-gap: 1rem; font-family: "Avenir", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } </style> <div id="cards"></div> <script> const profiles = [{ name: "Jane Doe", profession: "Framework Developer", motto: "I never wanted to be famous, I wanted to be great.", photo: "https://pymwoqn637.codesandbox.io/default.png" }, { name: "Kurtis Weissnat", profession: "Developer", motto: "When in doubt, iterate faster!", photo: "https://pymwoqn637.codesandbox.io/default.png" } ] const root = document.getElementById('cards') profiles.forEach(profile => { const card = document.createElement('ar-card') root.appendChild(card) card.profile = { ...profile } }) </script>
- Run your demo
npm run start.wc
- Browse app
http://localhost:8080/demo.html
- Run your demo
- Update
- Create new project using angular cli
ng new ng-vue-elements
- Run the project
cd ng-vue-elements ng serve --open
- Replace the content of your
./src/app/app.component.html
to:<div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> <img width="300" alt="Angular Logo" src="https://angular.io/assets/images/logos/angular/angular.svg"> </div>
- Update
./src/app/app.module.ts
- add
schemas
and provideCUSTOM_ELEMENTS_SCHEMA
to allow non angular component - add imports
CommonModule
to use common directivesi.e *ngFor
import { BrowserModule } from '@angular/platform-browser'; import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { CommonModule } from '@angular/common' import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, CommonModule ], providers: [], bootstrap: [AppComponent], schemas: [ CUSTOM_ELEMENTS_SCHEMA ] }) export class AppModule { }
- add
- Update
./src/app/app.component.html
<div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> <img width="300" alt="Angular Logo" src="https://angular.io/assets/images/logos/angular/angular.svg"> </div> <div class="cards"> <div *ngFor="let profile of profiles"> <ar-card [profile]="profile"></ar-card> </div> </div>
- Update your styles
./src/app/app.component.css
.cards { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: auto; grid-gap: 1rem; font-family: "Avenir", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; }
- Update
./src/app.component.ts
, add propertyprofiles
profiles = [ { name: "Jane Doe", profession: "Framework Developer", motto: "I never wanted to be famous, I wanted to be great.", photo: "https://pymwoqn637.codesandbox.io/default.png" }, { name: "Kurtis Weissnat", profession: "Developer", motto: "When in doubt, iterate faster!", photo: "https://pymwoqn637.codesandbox.io/default.png" }, { name: "Chelsey Dietrich", profession: "UX Developer", motto: "Genius is the ability to reduce the complicated to the simple.", photo: "https://pymwoqn637.codesandbox.io/default.png" }, { name: "Leanne Graham", profession: "UI Developer", motto: "The key to performance is elegance, not battalions of special cases.", photo: "https://pymwoqn637.codesandbox.io/default.png" } ]
- Copy the bundle of your vue-elements (from
<path-of-your-vue-elements-bundle>
to./src/assets/vue
) - Update
./src/index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>NgVueElements</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <script src="https://unpkg.com/vue"></script> <script src="/assets/vue/ar-card.min.js"></script> </head> <body> <app-root></app-root> </body> </html>
- Run your app
ng serve --open