Reputation: 1118
I'm using Nativescript-vue. I'm trying to put some logic in my app, that doesn't show any html or anything, its just a library that does calculations. I can't figure out how to just use it, i keep getting error messages.
Here is what I've tried.
rifle.js
export function Rifle () { // no function name
this.firstName = "hi";
}
also tried this and others:
const observableModule = require("tns-core-modules/data/observable");
function Rifle () { // no function name
const myRifle = observableModule.fromObject({
firstName:"hello"
})
return myRifle;
}
module.exports = Rifle;
then in my App.js file, i import it.
import {Rifle} from "./ballisticlibrary/rifle";
I've also tried to use Require instead.
I then try to use it in a vue method, invoked on button tap.
GetRifle()
{
// return "hi";
this.msg= this.Rifle.firstName;
}
Keeps throwing an error of :
Cannot read property 'firstName' of undefined"
System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method onClick failed
System.err: TypeError: Cannot read property 'firstName' of undefined
Upvotes: 1
Views: 53
Reputation: 3753
I have never used vue but I would expect the exported Rifle value to be a function or a class. That would suggest that you need to first create an instance of that class before using it. Also, I think that you cant access the imported classes using "this". you can try.
// your rifle class
export function Rifle () {
this.name = "AK-47";
this.damage = 5;
this.fire = () => {
console.log('Bang bang, '+ this.damage + ' damage done by ' + this.name);
}
}
// then import your rifle and instantiate it
const ak = new Rifle();
ak.fire();
// logs out Bang bang, 5 damage done by AK-47
Upvotes: 2
Reputation: 2465
The problem here is this.firstName
. Read this to better understand how to use this
in js.
Upvotes: 0