jsで位置情報を取得する

<div id="result"></div>

<script>
if(navigator.geolocation)
{
	navigator.geolocation.getCurrentPosition(

		function(position){

			var data = position.coords;

		var lat = data.latitude;
		var lng = data.longitude;
		var alt = data.altitude;
		var accLatlng = data.accuracy;
		var accAlt = data.altitudeAccuracy;
		var heading = data.heading;
		var speed = data.speed;

		document.getElementById('result').innerHTML = '<dl><dt>緯度</dt><dd>' + lat + '</dd><dt>経度</dt><dd>' + lng + '</dd><dt>高度</dt><dd>' + alt + '</dd><dt>緯度、経度の精度</dt><dd>' + accLatlng + '</dd><dt>高度の精度</dt><dd>' + accAlt + '</dd><dt>方角</dt><dd>' + heading + '</dd><dt>速度</dt><dd>' + speed + '</dd></dl>' ;

		var latlng = new google.maps.LatLng(lat, lng);

		var map = new google.maps.Map(document.getElementById('map-canvas'), {
			zoom: 15,
			center: latlng,
		});
			new google.maps.Marker({
				map: map,
				position: latlng,
			});
		},
		
		function(error)
		{
			var errorInfo = [
				"原因不明のエラーが発生しました。",
				"位置情報の取得が許可されませんでした。",
				"電波状況などで位置情報が取得できませんでした。",
				"位置情報の取得に時間がかかりすぎてタイムアウトしました。"
			];

			var errorNo = error.code;

			var errorMessage = "[エラー番号: " + errorNo + "]\n" + errorInfo[ errorNo ];

			alert(errorMessage);
			document.getElementById("result").innerHTML = errorMessage;

		},
		{
			"enableHighAccuracy": false,
			"timeout": 8000,
			"maximumAge": 2000,
		}

		);

}

else
{
	var errorMessage = "お使いの端末は、Geolocation APIに対応していません。";

	alert(errorMessage);

	document.getElementById('result').innerHTML = errorMessage;
}

</script>

なに!? vagrantだから?

facebook広告のセグメント

facebook広告のセグメントを見てみます。

基本設定
– 地域、年齢、性別、言語
1.地域・・・市区町村、指定した場所から半径〇km
いまその地域にいる人、その地域に住んでいる人、その地域にいた人
2.年齢・・・13歳~65際まで 1歳単位で設定可能
3.性別・・・男性、女性、両方(男性、女性すべて)
4.言語・・・国籍もセグメント可能

詳細ターゲット ~利用者層~
5.恋愛対象
6.交際ステータス
7.学歴
8.職歴
9.子どもの年齢
10.世代
11.ライフイベント

詳細ターゲット ~興味・関心~
スポーツ・アウトドア(アウトドア活動、スポーツ)、テクノロジー(コンピュータ、家電・エレクトロニクス)、ビジネス・業界(ビジネス、中小ビジネス、小売業、広告、オンライン、営業、マネージメント、マーケティング、起業、エンジニアリング、デザイン、経済、ファイナンス、不動産、建設、建築、医療、看護・介護、科学、航空、農業、銀行サービス、高等教育)、フィットネス・ウェルネス(エクササイズ、ジョギング、フィットネス、フィットネスクラブ、ダイエット、栄養、ヨガ、ズンバ、ウェイトトレーニング、ボディービルディング、瞑想)、レジャー施設(音楽、読書、映画、テレビ、ゲーム、ライブ・イベント)、家族と交際関係(友情、出会い関連、ウェディング、結婚、子育て、母親、父親、家族)、買い物・ファッション(衣料品、ファッションアクセサリー、ショッピング、美容、玩具)、趣味・アクティビティ(ペット、ホーム・ガーデニング、乗り物、旅行、政治・社会問題、時事)、食品・飲料品(飲料品、お酒、食品、料理、クッキング、レストラン)

コマンドラインからvue init

[vagrant@localhost vue]$ vue init webpack hpscript

? Project name hpscript
? Project description A Vue.js project
? Author
? Vue build standalone
? Install vue-router? Yes
? Use ESLint to lint your code? Yes
? Pick an ESLint preset Standard
? Set up unit tests Yes
? Pick a test runner noTest
? Setup e2e tests with Nightwatch? No
? Should we run `npm install` for you after the project has been created? (recom
mended) no

vue-cli · Generated “hpscript”.

# Project initialization finished!
# ========================

To get started:

cd hpscript
npm install (or if using yarn: yarn)
npm run lint — –fix (or for yarn: yarn run lint –fix)
npm run dev

Documentation can be found at https://vuejs-templates.github.io/webpack

中を見てみます。
ほう、jsファイルですね。

to get startedで、
cd hpscript
npm install (or if using yarn: yarn)
npm run lint — –fix (or for yarn: yarn run lint –fix)
npm run dev

ん!? サーバー立ち上がった?

はあ!? 表示されないんだが。
どういうことだ。

node.jsを使ってvue-cliを入れよう

まずnpmを使えるようにします。
[vagrant@localhost python]$ source ~/.nvm/nvm.sh
[vagrant@localhost python]$ nvm ls
-> v10.7.0
system
default -> 10.7.0 (-> v10.7.0)
node -> stable (-> v10.7.0) (default)
stable -> 10.7 (-> v10.7.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.3 (-> N/A)
lts/carbon -> v8.11.3 (-> N/A)
[vagrant@localhost python]$ nvm use 10.7.0
Now using node v10.7.0 (npm v6.1.0)
[vagrant@localhost python]$ node -v
v10.7.0
[vagrant@localhost python]$ npm -v
6.1.0

続いてvue-cliをインストール
[vagrant@localhost python]$ npm install -g vue-cli
npm WARN deprecated coffee-script@1.12.7: CoffeeScript on NPM has moved to “coffeescript” (no hyphen)
/home/vagrant/.nvm/versions/node/v10.7.0/bin/vue -> /home/vagrant/.nvm/versions/node/v10.7.0/lib/node_modules/vue-cli/bin/vue
/home/vagrant/.nvm/versions/node/v10.7.0/bin/vue-init -> /home/vagrant/.nvm/versions/node/v10.7.0/lib/node_modules/vue-cli/bin/vue-init
/home/vagrant/.nvm/versions/node/v10.7.0/bin/vue-list -> /home/vagrant/.nvm/versions/node/v10.7.0/lib/node_modules/vue-cli/bin/vue-list
+ vue-cli@2.9.6
added 253 packages from 220 contributors in 90.959s

2.9.6ですね。
[vagrant@localhost python]$ vue –version
2.9.6

vue.jsでrouting

var router = new VueRouter({
	routes: [
		{
			path: '/top',
			component: {
				template: '<div>トップページです。</div>'
			}
		},
		{
			path: '/users',
			component: {
				template: '<div>ユーザー一覧ページです。</div>'
			}
		}
	]
})

var app = new Vue({
	router: router
}).$mount('#app')

vue.jsでログインフォーム

var auth = {
	login: function(id, pass){
  	window.alert("login id:" + "\n" + "password:" + pass);
  }
}

var loginTemplate = `
	<div>
  	<input type="text" placeholder="ログインID" v-model="userid">
    <input type="password" placeholder="パスワード" v-model="password">
   </div>
`

Vue.component('user-login', {
	template: loginTemplate,
  data: function(){
  	return {
    	userid: '',
      password: ''
    }
  },
  methods: {
  	login: function(){
    	auth.login(this.userid, this.password);
    }
  }
});
<div id="login-template">
  <input type="text" placeholder="ログインID" v-model="userid">
  <input type="password" placeholder="パスワード" v-model="password">
  <button v-on:click="login">ログイン</button>
</div>

var headerTemplate = `
		<div style="background:white;">
    <slot name="header"></slot>
    </div>
`

Vue.component('page-header', {
	template: headerTemplate
});
<div>
  <page-header>
  <h1 slot="header">冬の果物</h1>
  </page-header>
  <ul>
  <li>りんご</li>
  <li>イチゴ</li>
  </ul>
</div>

vue.jsの親コンポーネントと子コンポーネント

var fruitsListChild = Vue.extend({
	template: '<h1>フルーツ一覧</h1>'
})

var fruitsListParent = Vue.extend({
	template: '<div>親コンポーネント<fruits-list-child></fruits-list-child></div>',
  components: {
  	'fruits-list-child': fruitsListChild
  }
})

new Vue({
	el: "#fruits-list",
  components: {
  	'fruits-list-parent': fruitsListParent
  }
})
<div id="fruits-list">
 <fruits-list-parent></fruits-list-parent>
</div>

var counterButton = Vue.component({
	template: '<span>{{counter}}個<button v-on:click="addToCart">追加</button></span>',
  data: function(){
  	return {
    	counter: 0
    }
  },
  method: {
  	addToCart: function(){
    	this.counter += 1
      this.$emit('increment')
    }
  },
});

new Vue({
	el: '#fruits-counter',
  components:{
  	'counter-button':counterButton
	},
  data: {
  	total: 0,
    fruits: [
    	{name: '梨'},
      {name: 'イチゴ'}
    ]
  },
  method: {
  	increment: function(){
    	this.total += 1
    }
  }
})
<div id="fruits-counter">
  <div v-for="fruit in fruits">
  {{fruit.name}}: <counter-button v-on:increment="increment()"></counter-button>
  </div>
  <p>合計: {{total}}</p>
</div>

vue.js リストレンダリング

var vm = new Vue({
	el: '#example', // document.getElementById('example'), $('#example')[0]
	data: {
  	items: [
    	{
				name: 'みかん',
        price: 100
      },
      {
      	name:'もも',
        price: 300
      },
      {
      	name:'いちじく',
        price: 500
      },
      {
      	name: 'メロン',
        price: 1000
      }
    ]
  }
})

window.vm = vm;
<div id="example">
  <li v-for="item in items">
  {{item.name}}は{{item.price}}円
  </li>
</div>

みかんは100円
ももは300円
いちじくは500円
メロンは1000円

validation

var vm = new Vue({
	el: '#example', // document.getElementById('example'), $('#example')[0]
	data: {
		name: '',  
  },
  computed: {
  	isValid: function (){
    	return this.name.length > 0;
    }
  }
});

window.vm = vm;

vue.js

<div id="example">
<p>{{name}}は{{price}}円</p>
</div>
var vm = new Vue({
	el: '#example', // document.getElementById('example'), $('#example')[0]
  data: {
  	name: 'みかん',
    price: 100
  }
})

window.vm = vm;

こうも書ける

var vm = new Vue({
	el: '#example', // document.getElementById('example'), $('#example')[0]
  data: {
  	name: 'みかん',
    price: 100
  },
  computed: {
  	priceWithTax: function(){
    	return this.price * 1.08;
    } 
  }
})

window.vm = vm;