Laravel + JS Vue - node.js

Just start a tutorial about Laravel 5.8 (from 0) and trying to add some JS to the project. Installed NodeJS: npm -v: 6.10.1; node -v: 12.6.0; Then install npm in the project and use npm run watch. After all change ExampleComponent.vue to MyButton.vue:
<template>
<div>
<button type="submit" class="my-button" v-text="test.name">My Button</button>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
axios.post('api/vue', {})
.then(response => {
this.test = response.data
});
},
data: function () {
return {
test: null,
}
},
props: [
'text'
]
}
</script>
<style>
.my-button{
background-color: #333333;
}
</style>
Register it in app.js:
require('./bootstrap');
window.Vue = require('vue');
Vue.component('my-button', require('./components/MyButton.vue').default);
let app;
app = new Vue({
el: '#app',
});
then in my view add:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<div id="app">
<my-button text="My new test button"></my-button>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
app.js component error
After all, I got the report from Laravel mix that build success:
DONE Compiled successfully in 16733ms 4:51:01 PM
But in the browser, I didn't see my button. I've tried cleaning the cache. But it doesn't help. Someone any suggestions?

Related

How to include an ejs in the script tag

I want to render ejs using the forEach loop inside the script tag but I get product is not defined error. I cant pass variable into ejs.render() function correctly.
Here is my ejs template for product card:
<div class="card mb-3" style="max-width: 540px">
<div class="row no-gutters">
<div class="col-md-4">
<img
src="/images/<%= product.image_groups[0].images[0].link%>"
class="card-img"
alt="<%= product.image_groups[0].images[0].alt%>"
/>
</div>
<div class="col-md-8">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><%= product.name %></h5>
<p class="card-text">
<%- product.short_description %>
</p>
<div class="card-footer bg-transparent border-dark mt-auto">
<div class="row">
<div class="col sm-6">
<p>Price:<%= product.currency %> <%= product.price %></p>
</div>
<div class="col sm-6">
More Info
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my script:
<script>
const form = document.querySelector("form");
form.addEventListener("submit", async (e) => {
e.preventDefault();
// get values
const productName = form.productname.value
const searchResultsEl = document.getElementById('searchResults')
try {
const res = await fetch("/search", {
method: "POST",
body: JSON.stringify({ productName }),
headers: { "Content-Type": "application/json" },
})
const foundProducts = await res.json()
foundProducts.data.forEach(product => {
let html = ejs.render("<%- include('../product/productCard.ejs') %>",{product:product})
searchResultsEl.innerHTML += html
})
} catch (err) {
console.log(err)
}
});
</script>
Error: product is not defined
I can print products by using console.log(product) so there are products. I cant figure out what is the problem. Any help?
Image of Error:
Try
let html = ejs.render('<%- include("../product/productCard.ejs") %>',{product:product})

Node w/ express using ejs: "ReferenceError: plants is not defined"

I'm getting this error in my when I load my .ejs view:
ReferenceError: plants is not defined
All of my other views work with the includes and such.
plants.js router:
router.get('/plants', plantsController.getPlantsPage);
plants.js controller:
const Plant = require('../models/plant');
exports.getPlantsPage = (req, res, next) => {
res.render('plants', {
plants: plants,
pageTitle: 'Plants',
path: '/plants',
hasPlants: plants.length > 0
});
};
plant.js model:
module.exports = class Plant {
constructor(common_name, image_url, scientific_name) {
this.common_name = common_name;
this.image_url = image_url;
this.scientific_name = scientific_name;
}
}
plants.ejs:
<% if (hasPlants) { %>
<% for (let plant of plants) { %>
<div class="col s12 m4">
<div class="card">
<div class="card-image waves-effect waves-block waves-light">
<img class="activator" src="<%= plant.image_url %>">
</div>
<div class="card-content">
<span class="card-title activator grey-text text-darken-4"><%= plant.common_name %><i class="material-icons right">MORE</i></span>
</div>
<div class="card-reveal">
<span class="card-title grey-text text-darken-4"><%= plant.common_name %><i class="material-icons right">CLOSE</i></span>
<p><%= plant.scientific_name %></p>
<p>Plant Details</p>
</div>
</div>
</div>
<% } %>
<% } else { %>
<div class="row">
<div class="col s12">
<p>No Plants Added!</p>
</div>
</div>
<% } %>
Thanks.
Did you make sure you set the plants variable? The error says plants is not defined. Try console.log() the variable.

Ember Js: Error when navigating between dynamic segment of a same route (Failed to excecute 'insertBefore')

I get an error when navigating between dynamic segment of the same route
for example when I navigate between /project/my-project/ to /project/my-project/gallery everything works fine.
But when I navigate from /project/my-project/ to /project/my-other-project
or /project/my-project/gallery to /project/my-other-project
Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The
node before which the new node is to be inserted is not a child of
this node
Uncaught (in promise) DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
This is part of my application route structure
which is written in router.js like this
this.route('project', {path: '/project/:seo_url'}, function() {
this.route('gallery');
});
this is what i wrote in project.hbs
<Project::Banner #title="{{this.model.name}}" #image="assets/images/projects/project-banner.jpg"/>
<section>
<div class="container">
<div class="tabs has-border-bottom">
{{#link-to "project.index" this.model.seo_url}}Description{{/link-to}}
{{#link-to "project.gallery" this.model.seo_url}}Gallery{{/link-to}}
{{#link-to "project.index" 'my-other-project'}}Error When I click this{{/link-to}}
</div>
</div>
</section>
{{liquid-outlet}}
<Home::ProjectSlider #title="Latest Projects" #projects={{this.model.latest_projects}}/>
project/index.hbs
<section>
<div class="container">
<div class="row">
<div class="col-sm-4">
placeholder content
</div>
<div class="col-sm-8">
<div class="project-detail-description">
<h5>Description {{this.model.seo_url}}</h5>
{{{this.model.description}}}
<div class="categories">
<h5>Scope of Work</h5>
{{#each this.model.categories as |category|}}
<div class="category">{{category}}</div>
{{/each}}
</div>
</div>
</div>
</div>
</div>
</section>
project/gallery.hbs
<section>
<div class="container">
<div class="row project-gallery index-items">
{{#each this.model.gallery as |picture|}}
<div class="col-sm-4">
<div class="project-item">
<a data-fancybox="gallery" href="{{env 'rootURL'}}{{picture}}">
<img src="{{env 'rootURL'}}{{picture}}">
<div class="gradient-overlay full"></div>
</a>
</div>
</div>
{{/each}}
</div>
</div>
</section>
Project::Banner and Home::ProjectSlider are components and I don't know if they are relevant to this problem.
UPDATE 1
I Tried removing
<Home::ProjectSlider #title="Latest Projects" #projects={{this.model.latest_projects}}/> from my project.hbs and I didn't get any error.
Here is the code of my home/project-slider.hbs
<section data-aos="fade-up" data-aos-duration="2000">
<div class="container">
<div class="clearfix pos-rel">
<h2 class="inline-block">{{#title}}</h2>
<LinkTo #route="projects" class="see-more-button">
<div class="pull-right"><div class="hidden-xs inline-block">See More</div> <FaIcon #icon="arrow-right" /></div>
</LinkTo>
</div>
</div>
<div class="banner">
<div id="projects-slider">
{{#each #projects as |project|}}
<div class="project-item">
{{#link-to 'project.index' project.seo_url}}
<img src="{{env 'rootURL'}}{{project.image}}" alt="{{project.title}}">
<div class="gradient-overlay"></div>
<div class="project-detail">
<h5>{{project.name}}</h5>
<div class="btn-flat alt2 inline-block"><span>See the work</span></div>
</div>
{{/link-to}}
</div>
{{/each}}
</div>
</div>
<div class="container has-border-bottom"></div>
</section>
The {{#each}} code is giving me errors and I'm clueless why.
UPDATE 2
I think slick slider initialization is what giving me errors
when I comment them, I don't get errors but obviously my slider is not working either.
here is the code of components/home/project-slider.js
I hope somebody would kindly look into it
import Component from '#ember/component';
export default Component.extend({
didInsertElement() {
// this._super(...arguments);
$('#projects-slider').slick({
slidesToShow: 4,
slidesToScroll: 3,
infinite: false,
responsive: [
{
breakpoint: 1440,
settings: {
slidesToShow: 3,
slidesToScroll: 2
}
},
{
breakpoint: 769,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
}
});

Aurelia: templating. Include a template in another

I would like to use two layouts for my Aurelia app.
Home - FAQ - Login page don't contain a sidebar;
But everything else do contain this sidebar.
My architecture, with a sidebar is pretty complicated in HTML and is like:
div.container
div.sidebar
div.sidebar_content
div.site_content
So i can't just enable or disable the sidebar because it contains the view.
I have to make two pages like "app.html", defined in main.js, but how tell to Aurelia "Choose app.html for this page, and app2.html for this other page"?
I have found the setRoot function but i have bugs with it (the routes does not work properly when i change setroot)
Thank you for your answers
The router supports the concept of "layouts." This is documented here: http://aurelia.io/hub.html#/doc/article/aurelia/router/latest/router-configuration/10
Basically, you specify a layout for each route (you can specify a default layout on the router-view custom element:
app.html
<router-view layout-view="./layout-with-sidebar.html"></router-view>
app.js
export class App {
configureRouter(config, router) {
config.title = 'Aurelia';
var model = {
id: 1
};
config.map([
{ route: ['home', ''], name: 'home', title: "Home", moduleId: 'home', nav: true },
{ route: 'no-sidebar', name: 'no-sidebar', title: "No Sidebar", moduleId: 'no-sidebar', nav: true, layoutView: 'layout-without-sidebar.html' }
]);
this.router = router;
}
}
layout-with-sidebar.html
<template>
<div class="container">
<div class="row">
<div class="col-sm-2">
<slot name="sidebar"></slot>
</div>
<div class="col-sm-10">
<slot name="main-content"></slot>
</div>
</div>
</div>
</template>
layout-without-sidebar.html
<template>
<div class="container">
<div class="row">
<div class="col-sm-12">
<slot name="main-content"></slot>
</div>
</div>
</div>
</template>
home.html
<template>
<div slot="sidebar">
<p>I'm content that will show up on the right.</p>
</div>
<div slot="main-content">
<p>I'm content that will show up on the left.</p>
</div>
</template>
no-sidebar.html
<template>
<div slot="main-content">
<p>Look ma! No sidebar!</p>
</div>
</template>

Vue.js - Working with Components

I am new to Vue.js. Very new so this question might sound a lot like a first graders. Forgive me.
I have the App.vue and Character.vue setup. I wanted to create characters on the fly and add them to an array (in App.vue) and let the rendering of the look/feel of the characters be done using Character.vue. The characters are created and added to the array and can be retrieved properly. Only thing is...Character.vue doesn't render them properly because for some reason, the character it retrieves from the array is undefined.
Help me?
Attaching files
App.vue
<template>
<div>
<div class='gameHeader'>
<h1>{{ gameHeader }}</h1>
</div>
<div class='gameMessages'>
<div class='gameMessage' v-for="msg in gameMessages">
{{ msg }}
</div>
</div>
<div class='gameMain'>
<button #click="rollNewCharacter">Roll New</button>
<div class="playerParty">
<character v-for="p in playerParty"></character>
</div>
<div class="computerParty">
<character v-for="c in computerParty"></character>
</div>
</div>
<div class='gameFooter'>
{{ gameFooter }}
</div>
</div>
</template>
<script>
import Character from "./assets/components/Character.vue";
export default {
components: { 'character': Character },
data: function(){ return { gameHeader: 'Monster Attack', gameMessages:[], playerParty: [], computerParty: [], gameFooter: '' }; },
methods: {
rollNewCharacter() {
var c = Character;
c.name = 'Usman';
c.type = 'Warrior';
c.mana = 100;
c.health = 100;
c.totalHealth = 100;
c.totalMana = 100;
console.log(c.name);
this.playerParty.push(c);
console.log(this.playerParty[0].name);
//this.computerParty.push(chr2);
}
}
}
</script>
Character.vue
<template>
<div class="character">
<span class='name'>{{ name }}</span><span class='type'>({{ type }})</span>
<div class='health'><div class='total' :class="totalHealth"><div class='health' :class="health"></div></div></div>
<div class='mana'><div class='total' :class="totalMana"><div class='mana' :class="mana"></div></div></div>
<span class='damage'>{{ damage }}</span>
<div class="actions">
<button #click="attack">Attack</button>
<button #click="specialAttack">Special Attack</button>
<button #click="heal">Heal</button>
</div>
</div>
</template>
<script>
export default {
props: [ 'name', 'type', 'mana', 'health', 'damage' , 'totalHealth', 'totalMana' ],
methods: {
attack: function(){},
specialAttack: function(){},
heal: function(){ alert(this.name); console.log(this);}
}
}
</script>
<style>
</style>
You should pass a prop when using the character component:
<character :character="p" v-for="p in playerParty"></character>
I have updated the character to receive only one prop:
export default {
props: [ 'character' ],
methods: {
attack: function(){},
specialAttack: function(){},
heal: function(){ alert(this.name); console.log(this);}
}
}
And this is the character component template with the new prop:
<template>
<div class="character">
<span class='name'>{{ character.name }}</span><span class='type'>({{ character.type }})</span>
<div class='health'><div class='total' :class="character.totalHealth"><div class='health' :class="character.health"></div></div></div>
<div class='mana'><div class='total' :class="character.totalMana"><div class='mana' :class="character.mana"></div></div></div>
<span class='damage'>{{ character.damage }}</span>
<div class="actions">
<button #click="attack">Attack</button>
<button #click="specialAttack">Special Attack</button>
<button #click="heal">Heal</button>
</div>
</div>
</template>

Resources