I want to pass an Laravel object to my Vue component. In Vue I want to parse the data to an JSON object and I already tried this solution.
In my blade I have this:
<creator :object="parseData({{ $object->toJson() }})"></creator>
And in my component I have this:
data() {
return {
object: null
}
},
methods: {
parseData(data) {
this.object= JSON.parse(data);
}
}
I also tried
props: ['object']
instead of data()
This gives me the following error:
[Vue warn]: Property or method "parseData" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
Can you tell me what I am doing wrong?
SOLVED
I tried it again today and for some reason it worked. Thanks for your time guys!
Your parseData method needs to be defined within the methods section of your JS.
Try:
data() {
return {
ball: null
}
},
methods: {
parseData(data) {
this.object= JSON.parse(data);
}
}
Edit: and do what #Piotr said :)
First: You didn't add property object to your component. To fix this you have to add this line
props: ['object'],
just before your data function.
Second: you have to define parseData function in methods part of your component. Fix it as follows:
methods: {
parseData () {
//your function logic
}
}
Vue needs to define methods, watchers, computed properties within proper section.
Now your code is compete.
Related
On my component init I'm getting data from the server
import {Rules} from "../../interfaces/interfaces";
rules: Rules
ngOnInit() {
this.tt = this.rulesService.getUserClientRules().subscribe(
t => {
console.log(t)
console.log(t.clientRules.canAll)
this.rules = t.clientRules
},
error => {
console.log(error.error.message)
}
)
}
My service code is
getUserClientRules(): Observable<Rules> {
return this.http.get<Rules>('/api/rules/getClientUserRules/')}
and I have interface like:
export interface Rules {
clientRules: any
}
I'm getting response like this:
{clientRules: {canAll: true, canSee: false}}
How I can push this object into my rules object? I want to use it like rules.canAll or rules.canSeeAll...
I need this strucrure rules { canAll: true, canSee: true } I need to use it for the checks like *ngIf="rules.canSee"
Thank you for your responses!!!
You can define props into Rules interface like this.
export interface Rules {
clientRules: { canAll: boolean, canSee: boolean, canSeeAll: boolean}
}
But if your response may have many variations I suggest you to use any type
Remove this.tt you dont need any. Simple do in subscription method. You dooing good.
Inside my webpack.config I use webpack-assets-manifest plugin to generate manifest json file. I need to change the key name inside json object so I customize property following instructions in the docs and this works perfect. What I wonder about is how is this working? Inside my manifest.json file localBundle and localBundle.map are generated. Isn't function supposed to end after it hits first return? How come both if statements return something?
webpack.config.js
module.exports = {
...
new WebpackAssetsManifest({
customize: (key, value) => {
if (value.toLowerCase().endsWith('.local.css')) {
return {
key: 'localBundle',
value: value
}
}
if (value.toLowerCase().endsWith('.local.css.map')) {
return {
key: 'localBundle.map',
value: value
}
}
}
}),
...
}
manifest.json
{
...
"localBundle": "stylesLocal.b035cc665aee76e41676ad101e93fd67.local.css",
"localBundle.map": "stylesLocal.b035cc665aee76e41676ad101e93fd67.local.css.map",
}
The customize callback runs multiple times which is why your conditional return values are used in manifest.json.
If you're curious, here is where customize is called.
I am trying to build an array of objects in VueJS and I am running into some issues with .$set and .$add.
For example, I need the following structure when adding new items/objects:
{
"attendees": {
"a32iaaidASDI": {
"name": "Jane Doe",
"userToken": "a32iaaidASDI",
"agencies": [
{
"name": "Foo Co"
}
]
}
}
}
New objects are added in response to an AJAX call that returns JSON formatted the same as above. Here is my Vue instance:
var vm = new Vue({
el: '#trainingContainer',
data: {
attending: false,
attendees: {}
},
methods: {
setParticipantAttending: function(data)
{
if (data.attending)
{
this.attendees.$add(data.userToken, data);
} else {
this.attendees.$delete(data.userToken);
}
}
}
});
This only works if I start with attendees: {} in my data but when I try attendees.length after adding an attendee, I receive undefined. If I use attendees: [], the new object does not appear to be added. And lastly, if I use .$set(data.userToken, data) it does not add in the 'token':{data..} format required.
What could be the issue here? What is the correct way to use $.add when starting with an empty array of objects?
UPDATE
I found that it works if I set attendees: {} and then, when adding a new object,
if (data.userToken in this.attendees) {
this.attendees.$set(data.userToken, data);
} else {
this.attendees.$add(data.userToken, data);
}
Not sure if there is a better way to accomplish this.
If you set attendees to an empty object ({}) it will not have a length attribute. That attribute is on Arrays.
If you set attendees to an empty array ([]) then you need to use $set (or really, I think you want .push()) – $add is intended for use on objects not on arrays.
I'm not quite following your last question – could you add more context?
EDIT 2:
The answer below was for Vue 1.x. For Vue 2.x and greater use:
this.$set(this.attendees, data.userToken, data);
More information here: https://v2.vuejs.org/v2/api/#Vue-set
EDIT:
Responding to your update, you should be able to just use $set in all cases. I.e. just do this:
this.attendees.$set(data.userToken, data);
As of version 0.6.0, this seems to be the correct way:
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })
http://vuejs.org/guide/reactivity.html#Change_Detection_Caveats
I want to populate some charting data from some JSON data pulled down using $http.get. However, the issue I have is the $scope property I am binding to doesn't exist until the JSON is returned, so the page is throwing an error when it loads.
How do I avoid the error? It feels like I have a chicken and egg scenario.
Example:
$scope.Model.Charts.Electricity = {
series: [
name: "2014 Target",
data: $scope.Model.Data.Json.Charts.Electricity.CurrentYearTarget
]
};
The Electricity.CurrentYearTarget property is the one that doesn't exist until the promise is completed:
promise.then(
function(payload) {
$scope.Model.Data.Json.Charts = payload.data;
});
The JSON is what then defines the objects that sit under .Charts. Example:
{"Electricity":
{"CurrentYearTarget":
[10000.0,
// snip
10000.0]
}
}
OK, so what can I do to work with this? I suppose I could wrap all of my properties like $scope.Model.Charts.Electricity and so forth into a simple JavaScript if statement, but that doesn't feel right.
I suggest you consider simplifying your approach somewhat, as $scope.Model.Data.Json.Charts.Electricity.CurrentYearTarget is somewhat verbose, and Model, Data, Json all mean the same thing really, you can probably cut some of these out.
However, this is beside the point, you can still acheive what you want to, just populate the data when the request has returned:
$scope.Model = { Data: { Json: { Charts: {} } } }
promise.then(
function(payload) {
$scope.Model.Data.Json.Charts = payload.data;
$scope.Model.Charts.Electricity = {
series: {
name: "2014 Target",
data: $scope.Model.Data.Json.Charts.Electricity.CurrentYearTarget
}
};
});
I am trying to pass a simple variable value into an HTML file using ember.js. My value is contained within a json file called value.json.
My HTML code is as follows:
<h1>I won {{App.moneyvalue}} today!</h1>
However when I pass the json call via ember, it think that the entire call is a variable:
App = Ember.Application.create({
moneyvalue: function () {
return $.getJSON( "js/value.json", function( data ) {
return data.tot;
});
}
}
And returns the following:
I won function () { return $.getJSON( "js/donors.json", function( data ) { return data.tot; }); } today!
As it seems to think that moneyvalue is a string variable as opposed to a value?
The jSON file is superbasic
{
"tot": 100
}
Where is this going wrong?
you're supplying Handlebars with a function, generally you would use a computed or normal property on the object. In this case you really just shouldn't define it in the application scope either, I'd recommend using an application route (it's the root route of your app).
App.ApplicationRoute = Ember.Route.extend({
model: function(){
return $.getJSON( "js/value.json");
}
});
Then in your handlebars just use
<h1>I won {{tot}} today!</h1>
Here's an example: http://emberjs.jsbin.com/OxIDiVU/576/edit