The Vue.js integration fails to function within Laravel 5.5's environment

When the + sign is clicked, I want to add a new text field to an existing form. This functionality works fine in my code snippet, but not on my Laravel 5.5 site.

No errors are showing up in my console.

The HTML code goes into create.blade.php and the Vue script is placed in addFlavor.vue. In my app.js file, I have added:

Vue.component('addflavor-component', require('./components/addFlavor.vue'));

// addFlavor.vue
new Vue({
    el: '#vue',
        data() {
            return {
            formdata: [],
                flavors: [{
                    name: '',
                    percentage: '',
                }]
            }
        },
        methods: {
            addAroma: function(){
                this.flavors.push({
                    name: '',
                    percentage: ''
                })
            }
        },
        
    })
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/vue/1.0.12/vue.min.js"></script>
<!-- create.blade.php -->
<div id="vue">
  
    <div class="form-row align-items-left" v-for="flavor in flavors">

        <div class="col form-inline">
            <label class="sr-only" for="flavorname">Aroma 1</label>
            <div class="form-group">
                <input  type="text" 
                        class="form-control mb-2 mb-sm-0" 
                        id="flavorname" 
                        v-model="flavor.name">
            </div>
            <div class="input-group md-2 mb-sm-0">
                <input  type="text" 
                        class="form-control" 
                        id="percentage" 
                        v-model="flavor.percentage">
                                
                <div class="input-group-addon">%</div>
            </div>
            <button class="btn btn-success mt-5 mb5" @click="addAroma">+</button>
        </div> 
</div>
  
  <hr>
  
  <pre>
    {{ $data | json }}
  </pre>
  
</div>

How can I successfully implement this script in my Laravel 5.5 site?

Answer №1

It is recommended to rename your file from addFlavor.vue to AddFlavor.vue in compliance with VueJS recommendations.

Modify the code in that file as follows:

export default {
    name: 'add-flavor',

    data() {
        return {
            formdata: [],
            flavors: [{
                name: '',
                percentage: '',
            }]
        }
    },

    methods: {
        addAroma() {
            this.flavors.push({
                name: '',
                percentage: ''
            })
        }
    },
}

Update the code in app.js to:

import AddFlavor from './components/AddFlavor.vue';

const app = new Vue({
    el: '#vue',
    components: {
        'addflavor': AddFlavor,
   }
});

Add the following line to webpack.min.js:

mix.js('resources/assets/js/app.js', 'public/js');
and include the app.js in your blade layout file using
<script src="{{ mix('/js/app.js') }}"></script>
.

Remove the manual import of VueJS from your HTML file. It is unnecessary since it is defined in package.json and will be installed via NPM. Also, ensure you are using the latest version 2.5.x of VueJS instead of version 1.x.

<!-- create.blade.php -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div id="vue"><!-- <-- This could go to your main layout file -->
    <add-flavor inline-template>
        <div class="form-row align-items-left" v-for="flavor in flavors">
            <div class="col form-inline">
                <label class="sr-only" for="flavorname">Aroma 1</label>
                <div class="form-group">
                    <input type="text" class="form-control mb-2 mb-sm-0" id="flavorname" v-model="flavor.name">
                </div>
                <div class="input-group md-2 mb-sm-0">
                    <input type="text" class="form-control" id="percentage" v-model="flavor.percentage">

                    <div class="input-group-addon">%</div>
                </div>
                <button class="btn btn-success mt-5 mb5" @click="addAroma">+</button>
            </div> 
        </div>
    </add-flavor>
    <hr>

    <pre>{{ $data | json }}</pre>

</div>

If not already completed, make sure to install all node dependencies by running npm install or yarn install, followed by node run watch to build all necessary modules.

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

What is the process behind __doPostback generating a URL on an ASP.NET website?

Can you please check out the following URL: When you select "Exhibitor A to Z" from the dropdown menu and click search, a javascript function is triggered for each link (javascript:__doPostBack('ctl00$MainContent$grdExhList$ctl00$ctl04$lnkExhLink&ap ...

Error Message: An unexpected token 'else' was encountered during the compilation of ejs files

Encountering an error message when attempting to implement dropdown functionality in the navbar. The error is as follows: SyntaxError: /Users/myfile/app/views/layouts/boilerplate.ejs:22 20| 21| <body class="d-flex flex-column vh-100" ...

using a dynamic v-model as an argument in a function call

I am completely new to using vuejs and currently working on creating a dynamic table. In this table, the left column will contain hidden input fields that will be used to query a database and display the results in a pop-up window for quick referencing. ...

Guide to generating a PDF file and utilizing a Node.js program for the download process

Seeking assistance in creating a button using my Node.js application to generate a downloadable PDF file filled with information from a PostgreSQL database. Any help is greatly appreciated! ...

Error in loop within Vue.js single file component

I'm experiencing an issue where I can't seem to make a loop work within a single .vue file. The error message I'm encountering is: The property or method "value" is not defined on the instance but referenced during render. Please ensure tha ...

Attempting to retrieve and save data in a variable using React Native

click here for image referenceI am a beginner in react-native and I recently attempted to use fetch to access an API. While I successfully managed to print the result on the console, I encountered difficulty in displaying it on a mobile screen. Below is a ...

AngularJS allows for the passing of a function value into another function

Is there a way for me to pass a value from one function to another? Here is an example of what I am trying to achieve: HTML <div> <li ng-repeat="language in languages" ng-click="myFirstFunction(firstValue) {{lang ...

Loading views and controllers on-the-fly in AngularJS

A new configuration tool is under development using Angular.JS. The user interface consists of two main sections: a left panel with a tree view listing all the configuration items and a right panel displaying screens for editing these items. There are appr ...

Is there a way to use AJAX to call only a specific PHP function without reloading the entire page

I am trying to retrieve data only from the php function instead of the entire page data. Here is the content of my demo.php file: <? echo "Whole Page data"; $name = $_POST['name']; if ($name == "demo"){ demo(); } ...

Efficiently handling jsonwebtoken errors in node and express

Here is the verification function I've created: exports.verifyToken = function(req, res, next){ var token = req.body.token; jwt.verify(token, config.sessionSecret, function(err, decoded) { if(err){ return next(err); }else{ ...

Is there a way to use findByIdAndUpdate() without inadvertently removing any existing data?

Imagine a scenario where there is a mongoose Person model structured as below: const PersonSchema = new mongoose.Schema({ name: String, address: { street: String, number: number } }); Now, consider a method designed for updati ...

Calculating the number of rows in a Laravel 4 database

Hello, I need assistance with retrieving the current count for my statement provided below. Unfortunately, I am only receiving the count and not the complete result: $admins = DB::table('users') ->select(DB::raw('count(users.id) as ...

Guide on adding up the value of a property within an array of objects using AngularJS

I am receiving an array of results from a Node.js API in my Angular app, and here is how it looks: <div *ngFor="let result of results"> <div *ngIf="result.harmattan.length > 0"> ... </div> <br> .. ...

Is it possible to utilize Javascript instead of PHP Curl to present JSON API data?

After successfully displaying JSON API data from openweathermap.org on my HTML webpage using PHP Curl, I am now seeking help to achieve the same output using Javascript. My PHP script retrieves JSON data from the source, and here is a snippet of that PHP c ...

What is the best way to include keys in my JSON data, rather than just values?

I've encountered an issue with the PrimeVue datatable I created, as it is only showing empty rows. After investigating, I believe the problem lies in my JSON data structure, where the fields do not have keys. What modifications should be made to stan ...

Identifying iOS 5 or above using JavaScript

I've been experimenting with this code snippet to check if the browser is iOS 5 or newer (found on this Stack Overflow thread Detect iOS version less than 5 with JavaScript). function iOSversion() { if (/iP(hone|od|ad)/.test(navigator.platform)) ...

What is the best way to mount a component in VueJS?

I am facing a situation where I have a component that is mounted as part of the DOM rendering process. The basic structure of the application looks like this: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

The PHP function is not successfully receiving a value from the AJAX call to be entered into the Database

I've been struggling with a piece of code lately, trying to pass the value 1 to my database when a user clicks the "collect coins" button. The goal is to reset the column "dailyfree" every day at 12 pm so that users can click the button again on the n ...

Traverse the nested dataLayer array to retrieve pipe-separated strings

Our booking platform includes a nested dataLayer variable. Users have the option to select one or multiple product types, and we are trying to extract a string that contains all the product types saved in an array. However, I am encountering an error when ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...