I am attempting to use a Parse server and am currently running into issues with initializing Parse in javascript and pointing to the correct serverURL. Curious if anyone else has had any luck doing this with Angular2 yet?
问题:
回答1:
I'm using Parse server within an Angular 1 application, but the code works for both. I'm initializing Parse in a own Parse service, which provides some handy helper methods. But I think it doesn't matter where you run this code.
var appId = 'appId';
var jsKey = 'jsKey'; // I think this is not used anymore
Parse.initialize(appId, jsKey);
Parse.serverURL = 'yourNewServerUrl.com';
回答2:
For frameworks that use typescript (ex. angular 2, ionic 2 & 3),
To solve the typescript read-only error:
Typescript Error
Cannot assign to 'serverURL' because it is a constant or a read-only property.
Parse.serverURL = 'http://localhost:1337/parse';
Use (Parse as any)
instead of Parse
to set the value of serverUrl such as:
import * as Parse from 'parse';
Parse.initialize('appId', 'jsKey'); // use your appID & your js key
(Parse as any).serverURL = 'http://localhost:1337/parse'; // use your server url
This require parse and @types/parse to be installed:
npm install --save @types/parse parse
回答3:
A bit late but I've found the trick when the typescript throws a Cannot assign to 'serverURL' because it is a constant or a read-only property
// Use parse with typescript
import * as Parse from 'parse';
// To set the server URL
let parse = require('parse');
Now you have access to parse.serverUrl
, which is the same as Parse.serverUrl
except that the TS definition will throw a read-only error.