1

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
2
  • 1
    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 . const rifle = new Rifle(); this.msg = rifle.firstName; Commented Feb 1, 2020 at 15:07
  • 1
    @RainerPlumer this worked, export function Rifle () { // no function name this.firstName= "hi"; function GetHelp (){ return "Help" } } Do you know why my function GetHelp wouldn't work though? Commented Feb 1, 2020 at 15:26

2 Answers 2

2
  • moved a comment into an answer for better formatting *

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I've been struggling to get this to work like this for awhile, and this did it.
0

The problem here is this.firstName. Read this to better understand how to use this in js.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.