0.1.1 • Published 3 years ago

ngx-vue v0.1.1

Weekly downloads
13
License
MIT
Repository
github
Last release
3 years ago

Usage

import {
  ChangeDetectorRef,
  Component,
  ComponentFactoryResolver,
  Input,
  OnChanges
} from '@angular/core';
import {computed, ref, SetupComp, OnSetup} from 'ngx-vue';

@Component({
  selector: 'test-setup',
  template: `
    <p>{{other}}</p>
    <p>{{comp}}</p>
  `
})
class TestComponent implements OnSetup extends SetupComp {
    // In order to get typechecking working, defaults like these are required
    // Don't worry - they'll be overwritten by the `@Setup` return
    helloMsg: string = '';
    mainNumber: number = 0;
    addToPlus: Function = () => {};
    @Input() hello = 'Hello';

    // These two items in your constructor are required, currently
    // https://github.com/oceanbit-dev/ngx-vue/issues/1
    constructor(cd: ChangeDetectorRef, componentFactoryResolver: ComponentFactoryResolver) {
        super(cd, componentFactoryResolver)
    }

    ngOnSetup(props) {
      const helloProp = toRef(props, 'hello');
  
      const helloMsg = computed(() => `${helloProp?.value?.substr(0, 5) || ''}, World`);
  
      const mainNumber = ref(12);
  
      const addToPlus = (): void => {
        mainNumber.value += 1;
        // This manual change detection is required currently, but soon will not be
        detectChanges();
      };
  
      return {
        helloMsg,
        mainNumber,
        addToPlus
      };
    }
}

Using Vue's Libraries

Yes, you can! Before you start, you need set alias in your build tool in order to redirect some apis from vue to ngx-vue to avoid memory leaks.

Aliasing

Currently, you have to utilize an Angular Custom Webpack Builder and follow the webpack instructions

Add following code to your webpack config

const config = { 
  /* ... */
  resolve: { 
    alias: { 
      'vue': 'ngx-vue',
      '@vue/runtime-dom': 'ngx-vue',
    },
  }
}