How can I link a Vue.js webpage with a WordPress site?

Looking to integrate a Vue.js 2 single page into an existing WordPress website? Perhaps you've already got an old WordPress site and now want to develop a new Vue tool or page that can be added seamlessly to the menu. How can this be achieved within WordPress?

Is it even possible? What are the necessary steps to make this integration happen?


I've crafted a custom template housing a basic div message.

new Vue({
        el: "#hello-world-app",
        data() {
          return {
            msg: "Hello World!"
          }
        }
      });
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="main-content" class="main-content">

<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">

    <div id="hello-world-app">
      <h1>{{ msg }}</h1>
    </div>


</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->

In functions.php, I have made the following additions:

    function my_enqueue_stuff() {
    if ( get_page_template_slug() == 'page-surveypage.php' ) {
        wp_enqueue_script('vue-dist', 'get_template_directory_uri() . '/js/vue.js, array (), 1.1, true );
        wp_enqueue_script('vue-survey', 'get_template_directory_uri() . '/js/survey.js, array (), 1.1, true);
    }
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );

Interestingly, upon loading the page with the custom template, all I get is {{msg}}. Could it be due to my decision to download the Vue script and place it in the js folder?

Answer №1

If you are looking to incorporate a Vuejs app into your website, consider creating a new template in your theme or child-theme. For a single page, you can create a specific page template named after the slug of the page where the Vuejs app will be displayed. If you plan to use the app on multiple pages, you can create a generic template file that authors can assign to any page, or use a shortcode to wrap your Vuejs component/app. Make sure to follow the naming conventions for template files as outlined here.

Once you have chosen your delivery method, remember to load the vuejs javascript file generated by your bundler, along with any additional dependencies required by your app.

I hope this information is helpful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Redirecting with Express js when the cookie is not found

I successfully integrated Facebook login using Passport-js and also set up Cookie-strategy for traditional username/password login on my Express-js backend with a React front-end. The backend and frontend are hosted on separate servers and domains (backend ...

Images in Next.js work properly in a local environment, but encounter issues in the production environment

My Next.js images are working fine on local, but when I try to push it to production, it crashes. Here is my code View my image component here This is where I map the images Here is the part of my .json file where I fetch data It works properly in loca ...

When a form is submitted, the PHP file is opened in a new page instead of running the script

I have developed a contact form that needs to be validated through ajax PHP before being sent via PHP email. However, upon clicking submit, the page refreshes and redirects to a new page: mypage.com/form_process.php Additionally, the stylesheet is not l ...

Utilize Bootstrap in an Angular template to effortlessly expand one element while collapsing all others

I'm facing an issue with a table in my component. Each row has a button that, when clicked, should expand to show some calculated data specific to that row. However, the problem is that when one button is expanded, it keeps other buttons expanded as w ...

Top method in Angular 6 for verifying if a scrollable section has been scrolled to the very bottom

I am searching for a reliable solution in Angular 6 to determine whether a scrollable host component has reached its maximum scroll bottom. Despite my efforts, I have been unsuccessful in finding a functioning example of a function that can detect if a cu ...

Having trouble getting CSS Animation to work in a React project?

I am currently developing a Next.js application using React. const partyButton = useRef(null) const handleParty = () => { setTimeout(() => { partyButton.current.classList.add('party-time'); console.log(party ...

I need help figuring out how to send a POST/GET request from AJAX to a custom module controller in Odoo 10, but I'm running into issues

I have implemented a custom module in Odoo 10 with a simple controller. Everything works smoothly when accessing http://127.0.0.1:8069/cmodule/cmodule through the browser, displaying the expected return string. However, I encountered an issue when attempt ...

Error: Attempted the use of 'append' method on an object lacking the implementation of FormData interface, with both processData and contentType set to false

Apologies for any English errors. I am attempting to use ajax to submit a form, and here is my JavaScript code: $("#formPublicidad").on('submit', function(event) { event.preventDefault(); var dataForm = new FormData(document.getElementBy ...

When transitioning between steps in Material UI React, the Vertical Stepper component should automatically scroll to the top of

When switching steps in Material UI's vertical stepper, it should automatically scroll to the beginning of the selected step. https://i.sstatic.net/fRX4E.png One potential solution is to utilize a ref to scroll to the stepper titles. More informatio ...

Incorporating a requirement into a function to ensure it is executed only under certain circumstances

I'm currently developing a text-based game using JavaScript that involves picking up a sword with the help of an in-game function. var takeSword = function() { if (currentRoom.hasSword) { $("<p>You picked up a sword.</p&g ...

Accessing JavaScript variables within Selenium

Currently utilizing Selenium Webdriver 2.0 to test an HTML page that incorporates an x.js file. Within x.js, the variable "price" is defined based on data entered from the HTML form and various Javascript calculations. In the HTML, the correct price valu ...

Is there a way to trigger the bootstrap datetimepicker when a custom button is clicked, while also storing the selected value in a hidden textbox

When the ADD button is clicked, a datetimepicker should open. After selecting a value from the datetimepicker, it should be saved in the textbox with ID #myTextBox. <div> <input id="myTextBox" type="text" ng-model="c" data-provide="datepicker ...

Difficulty in detecting variable modifications during testing - Angular2+

After successful login, I expect a certain variable to be updated with the appropriate value. However, during testing, all I see is 'undefined' returned. This variable does change when interacting with the app, showing an error message like &apo ...

What is the proper way to request permission for allowing others to access the mailto function?

I want to create a feature where users can open email on click using JavaScript. However, I have encountered an issue with using the mailto function in Chrome, as it requires changing the handlers settings to make it work. My query is whether it is possib ...

Canvas cannot be duplicated due to tainting, html2canvas

I have encountered an issue with my Snapshot button in HTML. Despite trying to add crossorigin="anonymous" to my script, I am still facing the problem of a Tainted Canvas when clicking the button. The button function is as follows: $('#snap').cli ...

What are the limitations of using a JS file within an Angular application?

I am struggling to integrate some js methods from a file into an angular application. Unfortunately, the browser is unable to recognize the js file. I have tried following the guidelines in this SO post, but the browser still cannot locate the file in the ...

Angular JS Tutorial: How to Nest ng-views

When merging two separate Angular apps into one, I encountered the need to nest ng-views. The structure of my sample code (index.html) looks like this: <!doctype html> <html lang="en" ng-app="myApp"> <head> <meta ch ...

Error: Unable to iterate over the elements of `this.state` as it is

NEW UPDATE I encountered an issue with the response being an array, but it was actually coming from the backend (Express/Build folder) Revisiting an old issue that I faced some time ago. In my development environment, everything works fine. But once I d ...

The client side script "/socket.io/socket.io.js" was not located, therefore the Express-generator project was used instead

I have been working on integrating the "chat-example" from Socket.IO's official website into an Express-generator generated project while keeping the structure of the project intact. I have made minimal changes to the code to fit it into the existing ...

The pagination feature for array field type is malfunctioning on Mongoose, yet it functions properly on the Mongo

I am facing an issue with pagination on the rating field of my product collection. After executing a query in the mongo shell, db.products.find({_id: ObjectId('610bd9233fdc66100f703dd4')}, {ratings: {$slice: [1,1]}}).pretty(); I received the ...