Using Vue.js with a Content Delivery Network (CDN) is quite straightforward. Here's a basic guide on how to do it:
html<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
Replace @2
with the version you want to use. At the time of writing, the latest stable version of Vue.js 2.x is being used. If you want to use Vue.js 3, you would include it similarly.
html<div id="app">
{{ message }}
</div>
<script>
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!'
}
})
</script>
This code creates a new Vue instance, binds it to the element with the ID app
, and sets up some data that Vue.js will manage. In this case, it's a message
variable.
my-component.js
, you would include it like this:html<script src="my-component.js"></script>
Remember that Vue components should be registered globally or locally within another Vue component.
v-bind
, v-model
, v-on
), computed properties, methods, lifecycle hooks, etc., just as you would when using Vue.js with other setups.That's it! You're now using Vue.js with a CDN. This setup is suitable for smaller projects or quick prototypes. For larger projects, consider using a build system like Vue CLI, which offers features like Single File Components, module bundling, and more robust development and build processes.