This sample/tutorial will walk through the integration of the following features in an Ionic Capacitor web/mobile application using the latest version Ionic Framework Vue.js Components and Vue.js, version three which is still in beta.
I am also using the latest beta release of the Ionic integration with Vue. I am using typescript in the example and relying heavily on the new pattern of composition when creating single file components.
A bit different than in the past, please see comments in the code below and the important points highlighted at end of section.
Main thing to notice, there is no data, methods, etc sections any more, it is all handled in the setup
<scriptlang="ts">// import components from ionicimport{IonContent,IonHeader,IonPage,IonTitle,IonToolbar,}from"@ionic/vue";// import capacitor pluginsimport{Plugins,CameraSource,CameraResultType}from"@capacitor/core";const{Camera}=Plugins;// import from vueimport{defineComponent,ref}from"vue";// import to get access to the routerimport{useRouter}from"vue-router";// define the componentexportdefaultdefineComponent({// provide the namename:"Home",// provide the list of components being used in the// templatecomponents:{IonContent,IonHeader,IonPage,IonTitle,IonToolbar,},// setup function, It is called after props resolution, when // instance of the component is created.setup(){constimageUrl=ref<string|null>();// get access to routerconstrouter=useRouter();// functionsconstnextPage=()=>{};consttakePicture=async()=>{};// return the props and functions to component// so they are accessible in the templatereturn{takePicture,imageUrl,nextPage,};},});</script>
Important Points
there is no access to this in the setup() function
// old waythis.$router.push("/next-page");// new wayconstrouter=useRouter();router.push("/next-page");
everything that is returned from the setup() function is available to use in the template.
values/properties defined using ref are accessible without a need to unwrap in template but you must use imageUrl.value when accessing in a function
<!-- in template --><divclass="ion-padding"><img:src="imageUrl ? imageUrl : null"/></div>
// in functionconsole.log(imageUrl.value);
Capacitor Plugins Support
Plugins are imported and utilized the same as they were in the previous version. The one difference you will notice is how the data properties are accessed.
Here is the code for using the Camera plugin in the application.
// importimport{Plugins,CameraSource,CameraResultType}from"@capacitor/core";const{Camera}=Plugins;// code inside of `setup()`consttakePicture=async()=>{try{constimage=awaitCamera.getPhoto({quality:90,allowEditing:true,resultType:CameraResultType.DataUrl,source:CameraSource.Prompt,});console.log("image",image);// image.base64_data will contain the base64 encoded // result as a JPEG, with the data-uri prefix added// unwrap to set the `value`imageUrl.value=image.dataUrl;// can be set to the src of an image nowconsole.log(image);}catch(e){console.log("error",e);}};
Capacitor PWA Support
Same as before just be sure to include the library and call defineCustomElements(window); after component is mounted