Malfunction of VueJS single-page application on WordPress admin menu page

The VueJS project was created using the Webpack template in vue-cli. When building for production, a static folder is generated with 2 subfolders and an index.html file. The subfolders include css and js, with one css file and three javascript files - app.xxxxxxx.js, manifest.xxxxxxxx.js, and vendor.xxxxxxx.js.

I deployed the production VueJS project on both a Node server and an Apache server. For the Node server, ExpressJS was used:

...
app.use('/public', express.static(__dirname + '/public'));
app.use('/static', express.static(__dirname + '/public/static'));
app.use('/js', express.static(__dirname + '/public/static/js'));
app.use('/css', express.static(__dirname + '/public/static/css'));
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/public/index.html'));
});
...

The single page application functions properly on the Node server.

As for the Apache server, I transferred the HTML content from the generated index.html to an index.php, ensuring the CSS and JS directories are accessible.

Now, I'm attempting to replicate this setup with WordPress without success.

In WordPress, I have loaded all JS and CSS files using wp_enqueue_script() and wp_enqueue_style() but struggle to integrate the HTML markup correctly with the CSS and JS files. This seems to be where the issue lies. What am I overlooking?

This is how the generated index.html structure looks like:

<!DOCTYPE html>
<html>
<head>
  <meta charset=utf-8>
... 
</body>
</html>

The provided code snippet from functions.php:

...

While the top-level menu and submenu appear correctly, clicking on the Sub Level Menu displays only text and not the VueJS SPA as expected.

Surely, the JS and CSS files are loading because upon inspecting the source code, the links to these files can be clicked displaying their contents.

The structure of CSS and JS files inside the twentyseventeen theme directory can be seen here:

https://i.sstatic.net/11k78.png

When defining the HTML content within the admin_sub_page_callback(), the code <div id="app"></div> was extracted from the original index.html page generated by the VueJS project.

Answer №1

Successfully integrating Vue.js widgets with WP is possible using browserify, rather than webpack.

Avoiding the browserify template of vue-cli, I opted for my own package.json configuration. Here's a summary: https://gist.github.com/maurop123/eb0c8e884fefe3e40c110b33beff48db

This setup generates a single file dist/bundle.js for the entire Vue app. After obtaining the file, I placed it among the static js files in my wp theme directory.

To link the script correctly, I included a wp_enqueue_script line within functions.php as shown below...

wp_enqueue_script( MD_THEME_NAME.'-bundle', MD_THEME_URI.'/assets/js/bundle.js', array('jquery'), NULL, true );

The arguments used in this function might vary based on the theme being used. Ensure that your wp_enqueue_script resembles an existing one in your theme.

Finally, inserting the necessary html element like

<div id="vueApp"></div>
into the current template completes the process.

I trust this information proves 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

Utilizing React-Redux along with Redux-Thunk in a functional component to dynamically modify the UI upon receiving data from an external API

Within my functional component, I am utilizing a custom hook to fetch data from an API using Redux. const useFetching = (someFetchActionCreator) => { const dispatch = useDispatch(); useEffect(() => { dispatch(someFetchActionCreator() ...

Triggering Submit Button Based on Checkbox Value in jQuery

Hey there, I'm encountering an issue. Let's say I have two types of checkboxes - one for person and one for company. If I submit the form without checking any of the person or company checkboxes, I want jQuery to display an alert. Otherwise, the ...

Guide to executing a script within an iFrame on the parent webpage?

If I have a webpage named Page B with a function called "test()", and then another webpage named Page A that includes an iFrame pointing to Page B, how can I trigger the execution of Page B's "test()" function from Page A? ...

Create a grid layout using Bootstrap and Vue that features responsive columns which resemble a snake pattern. The alternate rows should be displayed in reverse order for a visually dynamic

Having a bit of a tricky situation here, hoping someone can provide some insight. I am currently working on a project using Laravel, Vue, and Bootstrap where I need to loop through some objects and display them in a specific pattern. 1 2 3 6 5 4 7 8 9 Th ...

Navigating to a new state using Ionic can be done with the $state.go method within

I am experiencing an issue with Ionic routing after receiving a Push Notification. The notification contains a custom key, for example 'myKey'. When the user clicks on the notification message, it should open the application and the callback func ...

Combining and restructuring multidimensional arrays in Javascript: A step-by-step guide

I'm struggling with transforming a multidimensional array in JavaScript. Here is an example of the input array: [ [['a',1],['b',2],['c',3]], [['a',4],['d',2],['c',3],['x',5]], [[&a ...

Shuffle divs in a header using JavaScript and jQuery - Effortlessly arranging elements

I have successfully designed a captivating header, and now I am faced with the challenge of randomly placing star-shaped divs throughout it. These stars are implemented as spans with a 'star' class, which is stylized by CSS to create an actual st ...

Utilizing additional PHP files to handle the AJAX post request and subsequently execute the PHP script

I have been tasked with a complex assignment that I am struggling to comprehend. Let me provide a brief overview of what is required of me. There are three files involved in this task: 1. An HTML file that sends an AJAX post request to passwrapper.php. 2. ...

The library 'material-ui' does not have a 'withStyles' export available

Here is the content of my package.json file. Everything was working fine until recently when I started encountering this error. "@material-ui/icons": "1.0.0-beta.42", "chartist": "0.10.1", "classnames": "2.2.5", "material-ui": "1.0.0-beta.41", "npm-ru ...

After adding Angularjs code to my webpage, I am encountering an issue where I am unable to use the right-click function

I've been putting together a mock website to showcase my ideas, utilizing a blend of HTML5, Bootstrap, and AngularJS. Everything was smooth sailing until I introduced some AngularJS code - suddenly, the right-click menu ceased to function in Chrome. ...

Tips for incorporating jQuery val() into a span element!

I am currently working on a plugin that involves interacting with select elements grouped within a span. I want to enhance the functionality of my plugin by adding support for the val() function, which will allow users to easily get or set the 'value& ...

The presence of parentheses in a JQuery selector

My database contains divs with ids that sometimes have parentheses in them. This is causing issues with my JQuery selector. How can I resolve this problem? Let me provide an example to illustrate: https://jsfiddle.net/2uL7s3ts/1/ var element = 'hel ...

Ensure that the JavaScript file is fully loaded and that the JavaScript function has been properly initiated prior to executing any additional

Having an issue with a tracking code not working properly as it is called before a required JS script and function is loaded. The situation is as follows: Upon successful form submission (CF7 on WordPress), the following function is immediately called. ...

Trouble with navigating through the WordPress blog menu

Having trouble opening my blog menu item on my website. My "Blog" category is a top-level menu item, but when I click on it, the menu only opens on right click - "Open in new window". The site in question is . All other menu items work fine, except for th ...

Create a configuration featuring filter options similar to Notion's functionality

The objective is to create a system that can establish certain constraints, similar to the way Notion handles filter properties. https://i.sstatic.net/plctW.png System A sets up the constraints and System C evaluates them, both using Typescript. However, ...

Adding an item to an array within a local scope using an asynchronous callback

I am utilizing node.js on my server and using Redis as a key-store for storing character data. Each connection on my server has its own unique character assigned to it. My goal is to gather all the data about these characters (such as their name, age, prof ...

Is it possible to activate or deactivate a button depending on a radio button selection using jQuery?

Below is the code I have written in an aspx file: <asp:Button ID="btn" runat="server" Text="Add As a Sub Organization" OnClick="lnkChild_Click" ValidationGroup="GroupA" class="btn-u" CausesValidation="true" /> <asp:GridView ID="grdDuplicateO ...

Tips for implementing the .on method in React

I'm facing an issue with a component that utilizes the tagify library. The error message says that tagify.on is not recognized as a function. How can I implement the .on functionality in React? What is the correct way to incorporate these .on methods ...

Notification indicating that an object has been identified

Here is the code I am working with: const username = "john"; const password = "doe"; const url = '//api.bos2.cf/?type=verify&username=' + username + '&password=' + password + '&callback=?'; $.getJSON(url, functi ...

Attempting to remove options in a Multiple Choice scenario by selecting the X icon beside each one

I'm working on a multiple choice quiz and I'd like to add a button or icon resembling X in front of each option (even in front of the radio button), so that when I click on it, only that specific option is deleted. How can I achieve this? For in ...