What We Are Trying to Achieve
As a side project I am in the middle of porting a website from Angular.js to Vue.js. Why you may ask, well it is not a complicated site and even though I am proficient with AngularJS, I saw the jump to Angular too heavy and I don’t need all the functionality or complexity that introduces. I have worked with Vue.js before and find it a superb framework that allows you to get a site running quickly with just the basics and only include the parts of the framework you need. It is easy enough to come back and put more functionality in such as routing, state management later on.
So as part of this work I have large chunks of TypeScript that interact with jQuery UI that I need to bring over. I know I could write or find on GitHub some of this functionality, but this is supposed to be a quick project and I haven’t got the time. I just need to get Vue to interact with jQuery UI written in TypeScript. Should be easy…and it is.
Create the Component
Firstly a new TypeScript file is needed to place the jQuery UI functionality into. I am going to use the DatePicker in this example, but the process is pretty much the same with other widgets as well.
For TypeScript to understand what Vue is, you need to import Vue, then you extend the Vue instance adding the template and mounted functions. The mounted function is a lifecycle hook you can use for when the Vue instance is being instantiated.It allows you to start to interact with the DOM or Virtual DOM in the case of Vue.
In the above example you simply use this.$el to get jQuery to find the element as you would any other jQuery UI code. Looking at the DatePicker API, there is a onSelect function that gets called when the date is changed. To get Vue to use this date object, we need to emit it using self.$emit and a kebab case property that matches a property in the HTML.
In the HTML (actually Razor syntax in this case) we add the element like this:-
Where the same kebab cased property is used to call a function called calculateJulianDate. I have to use two @ symbols here other wise Razor will think it is a segment of Razor code.
Import Into Your Vue Page
So now we come to our page code. Generally I set out my Vue code so all I need to call is run the WebPack CLI command and it will pick up my app.js file (generated from app.ts).
Which includes both page and datepicker scripts.
So at the top of page.ts I again need to do an import, this time for DatePicker which works because we export it from the datePicker.ts file earlier.
We also need to extend the Vue instance similar to what we did earlier and register the component like this:-
Now we also need to add the actual function we specified earlier as a kebab case property, in this case I am calculating the Julian Date from the date object passed in so we use the Vue methods properties like this:-
I have left out the actual function code as it doesn’t add anything to this example, but the take home is it sets a data property which is then bound to the HTML like this:-