Trying to use v-model and v-show on radio buttons to show different divs (Vue and Laravel) - laravel

I'm trying to make different divs show up according to the selected radio button option using v-model and v-show but, even though they stay hidden when the page is loaded as they should, they don't show up when a radio button is selected.
This code is a Vue component inside of a Laravel project. When I try this same code on https://jsfiddle.net/, using only Vue, it works exactly how it should. But when I try it inside my Laravel project, it doesn't work.
Component's HTML (don't mind the id and name values):
<div id="app">
<div>
<span><strong>*</strong> options </span>
<div>
<input type="radio" id="grerjSim" name="grerj" value="showYes" v-model="showOption">
<label for="grerjSim"> yes </label>
</div>
<div>
<input type="radio" id="grerjNao" name="grerj" value="showNo" v-model="showOption">
<label for="grerjNao"> no </label>
</div>
<div v-show="showOption === 'showYes'">
<span><strong>*</strong> Lorem ipsum dolor sit amet. </span>
<input type="text" required>
</div>
<div v-show="showOption === 'showNo'">
<label for="grerjMotivo">No
<select id="grerjMotivo">
<option>Lorem ipsum dolor sit amet.</option>>
</select>
</label>
</div>
</div>
</div>
Component's JS:
<script>
new Vue({
el: '#app',
data: {
showOption: value=""
}
})
</script>
Any help would be great :)

I noticed that your Vue-related JavaScript is inside its own <script> tag, which I assume means you're writing the Vue code in your Blade template.
While you can get Vue apps working that way, it's easier to manage if you keep your JavaScript contained in the source files located in resources/js. There is even an example already there for you to customize.
Also, when I asked if you checked your console for any errors, I meant your web browser's developer tools console. If you haven't checked that yet, it may hold a very obvious clue in the form of a specific error.
Here are a couple errors that your console might have:
ReferenceError: Vue is not defined
This means you added the Vue app code (new Vue({ ... })), but no other JavaScript including the Vue library, so the code doesn't know what Vue is.
[Vue warn]: Property or method "showOption" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
If you tried to fix the undefined Vue error above by running Laravel Mix and adding a <script> tag for the built file, you might see this. That's because the default Mix source code has an example Vue app targeting #app already, so the two are conflicting and the one in app.js is winning.
You'll either want to remove your Blade file Vue app and customize the default one in app.js (recommended), or remove the app.js one and keep the Blade file.
Official documentation on using Laravel Mix with JavaScript.

Related

Laravel and Vue: How do I get images from my public folder in a Vue component?

I am trying to get a logo image from my public folder in my laravel and Vue application. The file is called index.vue and have the following structure:
<template>
<div>
<div class="header-container">
<div class="header-content">
<div class="logo-container">
<img :src="'/public/images/logo/black.png'" alt="logo">
</div>
</div>
</div>
<!-- Tell Vue where to render components -->
<router-view></router-view>
</div>
</template>
Here is the file structure and you can see that file exists and the path is correct:
But when I switch back to the view it looks like this:
I have also tried to call it the way I would in a blade template using {{ asset() }}, but this doesn't work and gives me a compiler error.
How do I get this image to load in my Vue file?
Not sure about vue + laravel. But, since its a relative path I dont think you need to bind anything.
<img src="/images/logo/black.png" alt="logo">

AMP form issue in a MVC framework

I am having an issue with a form using AMP.
The form works perfectly on a standard amp html page - the button is clicked and the search.php is triggered.
<form method="post" action-xhr="search.php" target="_top">
<input name="query" placeholder="search" id="query" size="40" value="">
<button type=submit class="btn-search"><amp-img ></amp-img></button>
<input type=hidden name=search value=1>
</form>
Then the exact same code (including all the includes, etc) on the MVC framework I am using doesn't work. The button is clicked and nothing happens.
Source codes on both are identical when viewing page source.
Should this not be happening - the specific MVC framework I am using possibly has an issue with this?
Or is it due to it being MVC, the view (header.twig) has the form on the not working version. Would the php code from search.php go into the controller?
Any help would be greatly appreciated!

Laravel blade.php use JavaScript compiled by mix to app.js

I'd like to use a JavaScript function in my blade.php file. I created the xyz.js file with a very simple function
function picture(soemthing){
document.getElementById('idXYZ').src = "example.com";
}
Then I added this file to the webpack.mix.js config:
mix.js(['resources/assets/js/app.js', 'resources/assets/js/xyz.js'], 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
and run: npm run dev. So far so good, npm compiled my script and the picture function was included in the app.js file. Now I'd like to add a form in blade.php and call the picture function on click. I created the following blade.php file:
#extends('layouts.app')
#section('content')
<div class="container">
<form>
Enter url: <input type="text" id="someID">
<input type="button" onclick="picture($('#someID').val())" value="Click"/>
</form>
</div>
<script src="{{ mix('js/app.js') }}"></script>
#endsection
And here I got really lost (btw: I was always the backend guy). On page reload browser displays
[Vue warn]: Error compiling template: Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as , as they will not be parsed.
And when I click the button I have
Error new:76 Uncaught ReferenceError: xyz is not defined
I understand that the first errors says that the app.js file was not loaded and that's the reason why picture is not found. How should I include app.js or any other compiled by mix JavaScript file? Is it really the tag causing this problem?
Try this I solved me
<script src="{{ asset('js/app.js') }}?time={{ time() }}"></script>
" I think not all browsers support this "
I think you're getting the error because you're placing a new script tag inside of the vue.js application div.
If you're opening your layout file app.blade.php there is a div with id="app" and inside it you're having your content section yield. Side-effect means that you shouldn't load a new script inside a Vue.js template.
The script should be in your layout at the end of the body tag. Also the script tag should look like this:
<script src="{{ asset('js/app.js') }}"></script>
Then your script should be available. But I would have a look at Vue.js and how you're handling click events there. click handler docs

Firefox requesting content inside HTML5 template before activation

I was debugging my web app using DevTools when I noticed that Firefox is requesting url defined in src attribute of an img that is located inside a HTML5 template tag, while Chrome doesn't.
I'm using the last version (50.0).
Is this a bug?
Example below:
<template id="object-img">
<div class='object-container'>
<div>
<img src="cacaca.jpg" class="img-responsive" alt="imagem" />
</div>
</div>
</template>
It's a kind of optimization from Firefox.
The image is requested only the first time if it is not already in the browser's history / cache. Subsequent reload of the page won't trigger the request again.
The problem is that the image inside the <template> is requested before the following but normal images in the rendered DOM, which could be counterproductive in some situations. A solution is to put the <template> after the other images.
Note: the template specifications state that the elements in the template are not rendered, but they are parsed. They say nothing about preloading of referenced resources.

Search-form action attribute with Advanced AJAX Page Loader wordpress plugin returns "more than one title tag on the page.." warning in debug mode

I'm trying to enable search functionality of the Advanced AJAX Page Loader plugin on a custom wordpress theme I'm building, but after I enter keyword into search and hit enter, loader.gif keeps on spinning and I get this message in debug mode : "WARNING: You seem to have more than one title tag on the page, this is going to cause some major problems so page title changing is disabled". It also passes the url of the website twice into the address bar and I believe this has something to do with the action attribute that's being passed by the form.
This is the site in question: http://natalija.co.nf
I left the debug mode of the AAPL plugin on for now
This is my searchform.php code:
<form method="get" id="searchform" class="searchform" action="<?php bloginfo('url'); ?>">
<input type="submit" id="searchsubmit" value="" class="btn" />
<input type="text" size="10" name="s" id="s" value="" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
</form>
I managed to ajaxify the theme with this plugin but I 'm having trouble getting the search to work properly so I was wondering if anyone here can help me out with this? It is driving me crazy.
I solved it. The code in search.php needs to be wrapped with a div class="searchform".

Resources